473,385 Members | 1,780 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

initializing array of structures

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
2 10183
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Chandrashekar Tippur | last post by:
All, I need to initialize an array of structures but I don't know how may elements are there. I tried to malloc the array but I am not sure how to initialize them. Snippet: struct myarray{...
10
by: Steve | last post by:
this code: private Array m_arrays = new Array; results in an array of 3 null Array objects. What am I missing?
3
by: farseer | last post by:
If i have an array of a certain type, is there away of initializing with without knowing it's type? for example (forgive me if some of this is syntactically incorrect) if i have a procedure...
31
by: arun | last post by:
suppose i have a pointer to an array of integers.can i initialize each member of the array using pointers?plz explain
10
by: padh.ayo | last post by:
Hi, I have a array structure called people It is properly initialized to 100 array structure elements. Now, I'm reading in from the command line a txt file, and I get them to open correctly. In...
7
blackstormdragon
by: blackstormdragon | last post by:
I just started working with arrays and was trying my hand at a two-dementional array. I dont understand why it keeps telling me I have to many initializers. char board= { "-","-","-",...
4
by: Peskov Dmitry | last post by:
class simple_class { int data; public: simple_class() {data=10;}; simple_class(int val) : data(val){} }; int main() {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.