Thread: Initialising struct array withing struct

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    3

    Question Initialising struct array withing struct

    Hi, I have the following 2 struct definitions:

    Code:
    typedef struct {
       int x;
       char s[];
    } a;
    
    typedef struct {
       int x;
       a s[];
    } b;

    How can I initialise an instance of b? I thought something like the following would work, but is doesn't.

    Code:
    static b zzz = {100, {{1, "one"}, {2, "two"} }};
    This will compile without error or warning, but only initialises zzz.s[0]. zzz.s[1] contains garbage.

    PS. No, this isn't homework.

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    You have to give your arrays a size; [] doesn't mean an array that will grow automatically.

    Actually in this case (last member of a struct) it's OK to not have an array size, but it doesn't do what you're trying. C is not like, say, Ruby, where strings grow automatically; you have to manage the memory yourself.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    3
    Quote Originally Posted by cas View Post
    You have to give your arrays a size; [] doesn't mean an array that will grow automatically.

    Actually in this case (last member of a struct) it's OK to not have an array size, but it doesn't do what you're trying. C is not like, say, Ruby, where strings grow automatically; you have to manage the memory yourself.
    I understand that you have to manage memory yourself. But I also know that the compiler will pre-define the size of an array based on the initialisers you give it. Take for example the following code..
    Code:
    char *lines[] = {"hello", "goodbye"};
    This declares an array of (char *) with 2 elements, and initialises them. I'm trying to do the same thing, but with arrays within structs. Is there any way of initialising array within structs like this?

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    3
    I just changed the char from an array to a pointer, and it seems to work now.

    Code:
    char *s;
    Isn't that strange.... If there were something wrong with the original, I'd have expected a compiler error.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Disco Elephant View Post
    I understand that you have to manage memory yourself. But I also know that the compiler will pre-define the size of an array based on the initialisers you give it. Take for example the following code..
    Code:
    char *lines[] = {"hello", "goodbye"};
    This declares an array of (char *) with 2 elements, and initialises them. I'm trying to do the same thing, but with arrays within structs. Is there any way of initialising array within structs like this?
    Well, yes, but that only works with something you can initialize when declared -- and you can't do that with a struct, since there's no way to put an initializer in the struct declaration itself, since that doesn't make any sense, and after that it's too late.

    The technical term for what you had in your first post is a "flexible array member", and such structs must be malloced to be used (which sounds onerous, but then your pointer version at the end is the same way).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  2. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  3. Array of struct pointers - Losing my mind
    By drucillica in forum C Programming
    Replies: 5
    Last Post: 11-12-2005, 11:50 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM