By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,666 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

initializing array of structures

P: n/a
Hi,
I am unable to intialize an array of structures and I keep getting an
error. Here is how the data structure looks like.

typedef struct
{
struct
{
int part_no;
char item_name[65];
}ItemList[10];

}MACHINE;

I also have an array of two elements each of which is of type MACHINE.

MACHINE category[2];

I want to initialise all the 10 elements of array ItemList for each
category as follows:

category[0].ItemList[] =
{
{2001, "SIDEBOLT"},
{2001, "DRILLBIT"},
--------
--------
};

When I try to initialize like the above, I get an error saying that
"parse error before ]".

What am I doing wrong and how do I correct this?

Thanks
Cheb.

Dec 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
ma*********@yahoo.com wrote:
Hi,
I am unable to intialize an array of structures and I keep getting an
error. Here is how the data structure looks like.

typedef struct
{
struct
{
int part_no;
char item_name[65];
}ItemList[10];

}MACHINE;

I also have an array of two elements each of which is of type MACHINE.

MACHINE category[2];
Initialization of category has to take place here, otherwise it
is no initialization!
MACHINE category[2] = {
....
};

I want to initialise all the 10 elements of array ItemList for each
category as follows:

category[0].ItemList[] =
Well, whatever it is, this definitely is not an initialization.
You are trying to assign something to an array. This is not
allowed. And the [] is only allowed for declaring an array
with an initializer or to declare a pointer in a parameter list.
Or, in C99, for a flexible array member.
{
{2001, "SIDEBOLT"},
{2001, "DRILLBIT"},
--------
--------
};

When I try to initialize like the above, I get an error saying that
"parse error before ]".

What am I doing wrong and how do I correct this?


What you _can_ do, is
category[0].ItemList[0].part_no = 2001;

In C99, you can use compound literals, i.e.
category[0] = (MACHINE) {
{2001, "SIDEBOLT"},
{2001, "DRILLBIT"},
--------
--------
};
However, not many compilers support C99 fully (yet).

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Dec 20 '05 #2

P: n/a
ma*********@yahoo.com wrote:
Hi,
I am unable to intialize an array of structures and I keep getting an
error. Here is how the data structure looks like.

typedef struct
{
struct
{
int part_no;
char item_name[65];
}ItemList[10];

}MACHINE;

I also have an array of two elements each of which is of type MACHINE.

MACHINE category[2];

I want to initialise all the 10 elements of array ItemList for each
category as follows:

category[0].ItemList[] =
{
{2001, "SIDEBOLT"},
{2001, "DRILLBIT"},
--------
--------
};

When I try to initialize like the above, I get an error saying that
"parse error before ]".


typedef struct
{
struct
{
int part_no;
char item_name[65];
} ItemList[10];

} MACHINE;

int main(void)
{

MACHINE category[2] =
{[0].ItemList = {
{2001, "SIDEBOLT"},
{2001, "DRILLBIT"},
}
};

return 0;
}

Dec 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.