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

1
  • You should have gotten tons of compiler errors, not just one... Commented Jun 10, 2011 at 8:15

6 Answers 6

5

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

did you forget quotes on your strings?

"age"

1 Comment

thanks for noticing, but that is not the problem ... I made an error while writing the example. will correct it.
0

Have you tried putting double quotes around your string constants?

Comments

0

Any suggestions?

Try:

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

1 Comment

Thank you. I seriously doubt my coding capabilities sometimes.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.