Getting error "expected expression before '=' token" for multiple lines similar to this...
help?
Code:struct Thing Subthing[3] = { "thisOne", 1, "*", FALSE, "thisTwo", 1, "*", FALSE, "thisThree", 1, "*", FALSE };
Getting error "expected expression before '=' token" for multiple lines similar to this...
help?
Code:struct Thing Subthing[3] = { "thisOne", 1, "*", FALSE, "thisTwo", 1, "*", FALSE, "thisThree", 1, "*", FALSE };
Are unmarried people a kind of "Global Variable"?
Wrap each of those lines in {}
Code:struct Thing Subthing[3] = { { "thisOne", 1, "*", FALSE }, { "thisTwo", 1, "*", FALSE }, { "thisThree", 1, "*", FALSE } };
This might vary from one compiler to the next but in Pelles C, the one I'm used to, you can't initialize unless you've used typedef.
That is to say you can only initialize at the same time as you create an instance of a type.Code:typedef struct tThing { char name[13]; int num; char dots[10]; int fav; } Thing, *pThing; Thing MyThing[3] = {"thisOne",1,"*",FALSE , "thisTwo",1,"*",FALSE , "thisThree",1,"*",FALSE };
Last edited by CommonTater; 10-10-2010 at 05:45 PM. Reason: added missing array dimension
thank you.
Are unmarried people a kind of "Global Variable"?
WTF is that supposed to mean? What I gave you works:
Code:#include <stdio.h> #define FALSE 0 struct Thing { char name[13]; int num; char dots[10]; int fav; }; int main() { struct Thing Subthing[3] = { { "thisOne", 1, "*", FALSE }, { "thisTwo", 1, "*", FALSE }, { "thisThree", 1, "*", FALSE } }; int i = 0; for (; i < sizeof(Subthing) / sizeof(Subthing[0]); ++i) printf("%s : %d : %s : %d\n", Subthing[i].name, Subthing[i].num, Subthing[i].dots, Subthing[i].fav); return 0; }Code:home:cs r2r$ gcc -Werror -pedantic -o thing thing.c home:cs r2r$ ./thing thisOne : 1 : * : 0 thisTwo : 1 : * : 0 thisThree : 1 : * : 0
JK...sorry...grouchy...
okay...probably a compiler issue. What are you using?
Are unmarried people a kind of "Global Variable"?
It's gcc on Mac OS X 10.6.4, but your original code compiled for me as written anyway, so my advice wasn't all that helpful in the end. In my opinion each individual struct initialization should be in its own set of brackets for clarity's sake. As stated above, you *must* do this at the initialization of the array. If you can't do it at initialization, then you must do it in a loop, individually setting the values of each struct's members.
You don't need the first dimension strictly speaking, if it's initialization you're doing. The compiler can and should figure it out.
It is not compiler dependent. If you haven't got it to work, you are probably mixing up the order of dimensions or indices.
If an array is being initialised, the compiler is required to infer the dimension from the initialisation list. This only works for the first dimension though and a common mistake is for people to expect other dimensions to be inferred - which they are not (which is also the reason for needing additional {} when initialising mult-dimensional arrays and structs). People with a basic background in mathematics often get caught by this, as indexing/dimensions of arrays in C is the reverse of what maths students are often taught to expect.
Last edited by grumpy; 10-10-2010 at 11:20 PM.