开发者

C : array of struct (which holds an int and another array of another struct)

开发者 https://www.devze.com 2023-03-12 02:33 出处:网络
struct dataStruct {const char* s;int num; }; struct Final_struct {int n;dataStruct a[]; }; Now the problem occurs when I try to initialize the Final_struct as followed:
struct dataStruct {     const char* s;     int num; }; 

struct Final_struct {     int n;     dataStruct a[]; }; 

Now the problem occurs when I try to initialize the Final_struct as followed:

const Final_struct Example[]= {
                                {100, { {"age", 20}, {"iq", 120}, {"bmi",26} } },
开发者_运维知识库                                {100, { {"age", 36}, {"iq", 145}, {"bmi",22} }}
};

It's a c code, and when I try to compile it gives the compiler error :

Fields of the object can not have arrrays of size 0

Any suggestions?

thank you.


dataStruct a[] defines the member of the struct as an array of size 0. This is practically useless. You need to specify its size in the definition of the struct because the compiler needs to know the size of the entire struct in advance.

Or, you can simply declare the field as dataStruct *a and then the array itself will not be contained in the struct.


If this is C, what is string ? Why do you expect to be able initialize it from what looks like other variables?

Try const char * for s, and initialize from quoted string literals.


did you forget quotes on your strings?

"age"


Have you tried putting double quotes around your string constants?


Any suggestions?

Try:

/* note the explicit array size for a[] */
struct Final_struct {     int n;     struct dataStruct a[3]; };


the array 'a' which is declared inside the structure named Final_struct should have the size. with out know the size of that variable the compiler can't assign the memory for that variable. so you should allocate the size first...

0

精彩评论

暂无评论...
验证码 换一张
取 消