initializing array of structures

M

mahesh_cbrl

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.
 
M

Michael Mair

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
 
M

Martin Ambuhl

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;
}
 

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
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top