Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > static (non-zero) initialization of an array of structs (array of length #define LEN)

Reply
Thread Tools

static (non-zero) initialization of an array of structs (array of length #define LEN)

 
 
anon.asdf@gmail.com
Guest
Posts: n/a
 
      08-27-2007
Hello!

In the following code-snippet, is it possible to initialize each
element of arr, with STRUCT_INIT?

struct mystruct {
int a;
char b;
};

#define STRUCT_INIT {5, 'a'}

#define LEN 3
struct mystruct arr[LEN] = \
{STRUCT_INIT};
/* does not work: only 1st element initialized as desired */

/*
I don not want to write
{STRUCT_INIT, STRUCT_INIT, STRUCT_INIT};
since LEN can change!
!!!!!!!!!
*/

..
..
Can this be done?

Thanks
-Albert

..
..
..
..

/****** test-program ******/
#define <stdio.h>
int main(void)
{
int i;

for (i = 0; i < LEN; i++)
printf("|%d|, |%c|\n", arr[i].a, arr[i].b);
return 0;
}

 
Reply With Quote
 
 
 
 
anon.asdf@gmail.com
Guest
Posts: n/a
 
      08-27-2007
On Aug 27, 1:02 pm, (E-Mail Removed) wrote:
> Hello!
>
> In the following code-snippet, is it possible to initialize each
> element of arr, with STRUCT_INIT?
>
> struct mystruct {
> int a;
> char b;
>
> };
>
> #define STRUCT_INIT {5, 'a'}
>
> #define LEN 3
> struct mystruct arr[LEN] = \
> {STRUCT_INIT};
> /* does not work: only 1st element initialized as desired */
>
> /*
> I don not want to write
> {STRUCT_INIT, STRUCT_INIT, STRUCT_INIT};
> since LEN can change!
> !!!!!!!!!
> */


Perhaps it can be done with the preprocessor.
I gave it a try, but I'm no preprocessor expert - the code below
results in compiler (preprocessor) errors.

But: Could something along these lines, be done:
#define STRUCT_INIT {5, 'a'}

#define PUT_A(n) \
#if ((n) == 1) \
STRUCT_INIT \
#elif ((n) > 0) \
STRUCT_INIT , PUT_B((n)-1) \
#endif

#define PUT_B(n) \
#if ((n) == 1) \
STRUCT_INIT \
#elif ((n) > 0) \
STRUCT_INIT , PUT_A((n)-1) \
#endif


#define LEN 3
struct mystruct arr[LEN] = \
{PUT_B(LEN)};


Thanks -Albert

 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      08-27-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) writes:

> On Aug 27, 1:02 pm, (E-Mail Removed) wrote:
>> In the following code-snippet, is it possible to initialize each
>> element of arr, with STRUCT_INIT?
>>
>> struct mystruct {
>> int a;
>> char b;
>>
>> };
>>
>> #define STRUCT_INIT {5, 'a'}
>>
>> #define LEN 3
>> struct mystruct arr[LEN] = \
>> {STRUCT_INIT};

<snip>
>
> Perhaps it can be done with the preprocessor.

<snip>
> #define STRUCT_INIT {5, 'a'}
>
> #define PUT_A(n) \
> #if ((n) == 1) \
> STRUCT_INIT \
> #elif ((n) > 0) \
> STRUCT_INIT , PUT_B((n)-1) \
> #endif
>

<snip>

You are putting a lot of effort to avoid the simplest for loop. Why?
Just initialise your data in a loop.

--
Ben.
 
Reply With Quote
 
anon.asdf@gmail.com
Guest
Posts: n/a
 
      08-27-2007
On Aug 27, 1:47 pm, Ben Bacarisse <(E-Mail Removed)> wrote:
<snip>
> You are putting a lot of effort to avoid the simplest for loop. Why?

Good question - it made me think!
Here's a scenario: If you have code running at boot time and want to
boot as fast as possible!

> Just initialise your data in a loop.

True. -Albert

 
Reply With Quote
 
Abdo Haji-Ali
Guest
Posts: n/a
 
      08-27-2007
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> On Aug 27, 1:47 pm, Ben Bacarisse <(E-Mail Removed)> wrote:
> <snip>
>> You are putting a lot of effort to avoid the simplest for loop. Why?

> Good question - it made me think!
> Here's a scenario: If you have code running at boot time and want to
> boot as fast as possible!

How big is this array anyway? I'd say that if it's less than 1 MB it should
take less than a second. I think that performance should only be considered
when the code is run very frequently and is time-consuming. Not when you're
running it just once and at the initialization phase where users expect the
program to spend some time (a second or two) to initialize.

Abdo Haji-Ali
Programmer
In|Framez


 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      08-27-2007
(E-Mail Removed) wrote On 08/27/07 09:30,:
> On Aug 27, 1:47 pm, Ben Bacarisse <(E-Mail Removed)> wrote:
> <snip>
>
>>You are putting a lot of effort to avoid the simplest for loop. Why?

>
> Good question - it made me think!
> Here's a scenario: If you have code running at boot time and want to
> boot as fast as possible!


Which do you think is faster: Running a loop that
copies constant data into a million array slots, or
reading a million copies of that data from the boot
device?

--
(E-Mail Removed)
 
Reply With Quote
 
Duncan Muirhead
Guest
Posts: n/a
 
      08-27-2007
On Mon, 27 Aug 2007 11:02:00 +0000, anon.asdf wrote:

> Hello!
>
> In the following code-snippet, is it possible to initialize each
> element of arr, with STRUCT_INIT?
>
> struct mystruct {
> int a;
> char b;
> };
>
> #define STRUCT_INIT {5, 'a'}
>
> #define LEN 3
> struct mystruct arr[LEN] = \
> {STRUCT_INIT};
> /* does not work: only 1st element initialized as desired */
>
> /*
> I don not want to write
> {STRUCT_INIT, STRUCT_INIT, STRUCT_INIT};
> since LEN can change!
> !!!!!!!!!
> */
>

<snip>
If you can put this in a separate C file, you could write
a wee program to write that C file, given LEN, and invoke it
as part of your build process.

Off topic: if you use gcc and don't care about portability to other
compilers, you could write
struct mystruct arr[] = { [0 ... LEN-1 ] = STRUCT_INIT};
That's not C but a gcc extension.

 
Reply With Quote
 
Dave Hansen
Guest
Posts: n/a
 
      08-27-2007
On Aug 27, 6:47 am, Ben Bacarisse <(E-Mail Removed)> wrote:
> (E-Mail Removed) writes:
> > On Aug 27, 1:02 pm, (E-Mail Removed) wrote:
> >> In the following code-snippet, is it possible to initialize each
> >> element of arr, with STRUCT_INIT?

>
> >> struct mystruct {
> >> int a;
> >> char b;

>
> >> };

>
> >> #define STRUCT_INIT {5, 'a'}

>
> >> #define LEN 3
> >> struct mystruct arr[LEN] = \
> >> {STRUCT_INIT};

> <snip>
>
> > Perhaps it can be done with the preprocessor.

> <snip>
> > #define STRUCT_INIT {5, 'a'}

>
> > #define PUT_A(n) \
> > #if ((n) == 1) \
> > STRUCT_INIT \
> > #elif ((n) > 0) \
> > STRUCT_INIT , PUT_B((n)-1) \
> > #endif

>
> <snip>
>
> You are putting a lot of effort to avoid the simplest for loop. Why?
> Just initialise your data in a loop.
>


All the world is not a hosted system. And many of us deal with
software that is not loaded from disk, memory that cannot be written
at runtime, and RAM that is scarce and expensive. Of course, in those
situations, you rarely (never?) want all the elements of the array to
contain identical data.

This is one of C's few real shortcomings for writing embedded
software.

The standard way to (or at least, the way I usually) overcome this is
to #define LEN in a header file, and write a simple PC application
that can be invoked by the makefile to generate the initialization
data and save it to a file, so the code becomes

#include "len.h"

struct mystruct arr[LEN] = {
#include "initarr.dat"
};

Regards,

-=Dave

 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      08-27-2007
Eric Sosman wrote, On 27/08/07 16:04:
> (E-Mail Removed) wrote On 08/27/07 09:30,:
>> On Aug 27, 1:47 pm, Ben Bacarisse <(E-Mail Removed)> wrote:
>> <snip>
>>
>>> You are putting a lot of effort to avoid the simplest for loop. Why?

>> Good question - it made me think!
>> Here's a scenario: If you have code running at boot time and want to
>> boot as fast as possible!

>
> Which do you think is faster: Running a loop that
> copies constant data into a million array slots, or
> reading a million copies of that data from the boot
> device?


It depends. And there are situations I am aware of which almost
certainly do *not* apply to the OP where the bootup time *is* critical.
--
Flash Gordon
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      08-27-2007
Dave Hansen <(E-Mail Removed)> writes:

> On Aug 27, 6:47 am, Ben Bacarisse <(E-Mail Removed)> wrote:
>> (E-Mail Removed) writes:
>> > On Aug 27, 1:02 pm, (E-Mail Removed) wrote:
>> >> In the following code-snippet, is it possible to initialize each
>> >> element of arr, with STRUCT_INIT?

<lost of macro stuff>
>>
>> You are putting a lot of effort to avoid the simplest for loop. Why?
>> Just initialise your data in a loop.
>>

>
> All the world is not a hosted system.


Yes, it was a genuine question. I thought it prudent to check, first,
that there *was* reason to go to all the effort of statically
initialising the array.

--
Ben.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Static initialization of an array of Structs (each of which has anarray member) mike.arsenault C++ 4 11-25-2009 03:06 PM
Array of structs instead of an array with pointers to structs? Paminu C Programming 5 10-11-2005 07:18 PM
length of an array in a struct in an array of structs in a struct in an array of structs Tuan Bui Perl Misc 14 07-29-2005 02:39 PM
const structs in other structs Chris Hauxwell C Programming 6 04-27-2004 07:03 PM
structs with fields that are structs Patricia Van Hise C Programming 5 04-05-2004 01:37 AM



Advertisments