Thread: struct init

  1. #1
    False Return hubris's Avatar
    Join Date
    Apr 2009
    Posts
    33

    struct init

    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"?

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Wrap each of those lines in {}

    Code:
    struct Thing Subthing[3] =
        {
            { "thisOne", 1, "*", FALSE },
            { "thisTwo", 1, "*", FALSE },
            { "thisThree", 1, "*", FALSE }
        };

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by hubris View Post
    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
    	};
    How is your "thing" structure internally defined before you initialize it?
    C needs to know what it's assigning to what.

  4. #4
    False Return hubris's Avatar
    Join Date
    Apr 2009
    Posts
    33
    Quote Originally Posted by CommonTater View Post
    How is your "thing" structure internally defined before you initialize it?
    C needs to know what it's assigning to what.
    Code:
    struct Thing {
    		char name[13];
    		int num;
    		char dots[10];
    		int fav;
    	};
    Are unmarried people a kind of "Global Variable"?

  5. #5
    False Return hubris's Avatar
    Join Date
    Apr 2009
    Posts
    33
    Quote Originally Posted by rags_to_riches View Post
    Wrap each of those lines in {}

    Code:
    struct Thing Subthing[3] =
        {
            { "thisOne", 1, "*", FALSE },
            { "thisTwo", 1, "*", FALSE },
            { "thisThree", 1, "*", FALSE }
        };
    How about I wrap the whole project in ten or twenty extra brackets instead?
    Are unmarried people a kind of "Global Variable"?

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by hubris View Post
    Code:
    struct Thing {
    		char name[13];
    		int num;
    		char dots[10];
    		int fav;
    	};
    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.

    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 };
    That is to say you can only initialize at the same time as you create an instance of a type.
    Last edited by CommonTater; 10-10-2010 at 05:45 PM. Reason: added missing array dimension

  7. #7
    False Return hubris's Avatar
    Join Date
    Apr 2009
    Posts
    33
    thank you.
    Are unmarried people a kind of "Global Variable"?

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Quote Originally Posted by hubris View Post
    How about I wrap the whole project in ten or twenty extra brackets instead?
    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

  9. #9
    False Return hubris's Avatar
    Join Date
    Apr 2009
    Posts
    33
    JK...sorry...grouchy...
    okay...probably a compiler issue. What are you using?
    Are unmarried people a kind of "Global Variable"?

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    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.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by rags_to_riches View Post
    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.
    As it turns out, I agree... the extra bracketing doesn't hurt anything and it's a lot more concise.

    Also, there was a small error in my suggestion... I forgot to include the array dimension of [3]...

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You don't need the first dimension strictly speaking, if it's initialization you're doing. The compiler can and should figure it out.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by whiteflags View Post
    You don't need the first dimension strictly speaking, if it's initialization you're doing. The compiler can and should figure it out.
    This might be compiler dependent... I've never gotten it to work without the array size.

  14. #14
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by CommonTater View Post
    This might be compiler dependent... I've never gotten it to work without the array size.
    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.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. casting struct to struct
    By klmdb in forum C Programming
    Replies: 6
    Last Post: 08-14-2010, 02:29 PM
  2. Cant understand Linked list example:
    By satty in forum C Programming
    Replies: 15
    Last Post: 08-13-2010, 10:12 AM
  3. C problem - Create a Personal Library
    By Harliqueen in forum C Programming
    Replies: 33
    Last Post: 04-20-2010, 11:27 PM
  4. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  5. linked list question
    By brb9412 in forum C Programming
    Replies: 16
    Last Post: 01-04-2009, 04:05 PM