Click to See Complete Forum and Search --> : explicitly defining each member of an array of structs


rabidusv
October 4th, 2002, 10:57 AM
How would I make an array of structs then explicitly define each member of each struct in the array? For example:


struct test
{
char member1;
char member2;
};

typedef test array[2];

array real_var={
{ a, b }
{ c, d }
};


This obviously isn't right, any help or suggestions?

PaulWendt
October 4th, 2002, 11:05 AM
Hello there. You are very close:

struct test
{
char member1;
char member2;
};

typedef test array[2];

array real_var={
{ 'a', 'b' },
{ 'c', 'd' }
};


To initialize a struct, you just initialize each of its members in a
comma-separated list ... all within curly brackets. The same thing
is done with arrays. That's why you see a comma between the
first struct and the second struct.

The only other problem you had was not putting apostrophes
around your char's. The way to specify a char data type is to
put apostrophes around them; that's why you see 'a', 'b', etc....

--Paul