Initialising structures

N

Nidhi

Hi all,
I dont know whether this question has been raised before or not.
Nevertheless, here is my question...

I have a structure of around 255 elements of the same type, say int.
I have an array of this structure of around 100 elements. I want to
initialise all the 100 elements of this array (and the 255 elements
too) to some specific value (a non-zero value). Is there any way of
doing it without using loops?

Thanks in advance.
 
V

Vijay Kumar R Zanvar

[..]
I have a structure of around 255 elements of the same type, say int.
I have an array of this structure of around 100 elements. I want to
initialise all the 100 elements of this array (and the 255 elements
too) to some specific value (a non-zero value). Is there any way of
doing it without using loops?

Thanks in advance.

#include <stdio.h>
#include <string.h>

#define NON_ZERO 5
struct {
int _1;
int _2;
...
int _255;
}large_struct[100];

int
main ( void )
{
memset ( large_struct, NON_ZERO, sizeof large_struct );
return 0;
}

Is bzero(3) defined by the standard?
 
L

loren

Nidhi said:
Hi all,
I dont know whether this question has been raised before or not.
Nevertheless, here is my question...

I have a structure of around 255 elements of the same type, say int.
I have an array of this structure of around 100 elements. I want to
initialise all the 100 elements of this array (and the 255 elements
too) to some specific value (a non-zero value). Is there any way of
doing it without using loops?

Thanks in advance.
Well, it depends how they're allocated...

If they're local variables allocated on the stack i believe you can use
= {0} to initialize every element to 0. memset and bzero won't work,
they're only guranteed to work on byte sized variables, everything else
is undefined.

The other option, if they're dynamically allocated, is to create a
template structure on the stack locally and templatestruct = {0} to
initialize it to zero, and then loop through the array of structures and
set each structure = templatestruct.
 
S

Sheldon Simms

Yes, you can simply type the entire gigantic mess out by hand:

struct foo { /* 255 members */ } bar[100] =
{{2,2,...,2,2},{2,2,...,2.2},...,{2,2,...,2,2}};

Other than that, I think a loop setting up the array is your
best option. If you don't want 255 lines of s.mem1 = SOMEVAL;
in your loop, you can set up one "template" struct either
with an initializer, as above, and then copy the template into
each element of the array:

#define ARRAY_SIZE 100
struct foo { /* 255 members */ };
struct foo foo_template = {2,2,...,2,2};
struct foo bar[ARRAY_SIZE];
int idx;
....
for (idx = 0; idx < (sizeof bar / sizeof bar[0]); ++idx)
bar[idx] = foo_template;
....
#include <stdio.h>
#include <string.h>

#define NON_ZERO 5
struct {
int _1;
int _2;
...
int _255;
}large_struct[100];

int
main ( void )
{
memset ( large_struct, NON_ZERO, sizeof large_struct );

I doubt about this.

It's good to see that you changed your mind, because what you suggested
was ridiculous. It sets the value of every byte of the array to 5.
What that means for the values of the struct members is anyone's guess.

No. It's not all that useful for clearing structs anyway.
 
G

goose

Vijay Kumar R Zanvar said:
[..]
I have a structure of around 255 elements of the same type, say int.
I have an array of this structure of around 100 elements. I want to
initialise all the 100 elements of this array (and the 255 elements
too) to some specific value (a non-zero value). Is there any way of
doing it without using loops?

I suspect that a macro is the only alternative to loops;
of course this implies that you have to type out the entire
initialisation *at least once* ... see below.
Thanks in advance.

#include <stdio.h>
#include <string.h>

#define NON_ZERO 5
struct {
int _1;
int _2;
...
int _255;
}large_struct[100];
#define SET_STRUCT(structure,value) do {\
structure._1 = value; \
structure._2 = value; \
structure._3 = value; \
... \
structure._255 = value; \
} while (0)

#define SET_ARRAY(array,value) do{\
SET_STRUCT (array[0], value); \
SET_STRUCT (array[1], value); \
SET_STRUCT (array[2], value); \
... \
SET_STRUCT (array[99], value); \
} while (0)
int
main ( void )
{
memset ( large_struct, NON_ZERO, sizeof large_struct );

/* ***********
this does not work if sizeof (int) != sizeof (char), you are
setting *each byte* of each struct to 5.

example:
if int is 4 bytes, then instead of setting it to :
byte_1 byte_2 byte_3 byte_4
0 0 0 5

or setting it to :
byte_1 byte_2 byte_3 byte_4
0 5 0 0

you are setting it to
byte_1 byte_2 byte_3 byte_4
5 5 5 5

instead use the macros above and do this:
*********** */
SET_ARRAY (large_struct, NON_ZERO);
return 0;
}

Is bzero(3) defined by the standard?

no, it isn't (possibly posix, or BSD)

hth
goose,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top