Thread: arrays of structs

  1. #16
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by echo49
    hi there
    i'm new to C programming, and there's this code i'm trying to get running
    What you want is this:
    Code:
    typedef struct
    {
    	char word[ SOMESIZE ];
    } dictionary;
    
    dictionary dict[749] =
    {
        { "account" },
        { "act" },
        { "addition" },
        { "adjustment" },
    
        //...
    
        { "wrong" },
    };
    Arrays of structures can be initialized like other arrays, as above, provided it's done at the time of declaration. Otherwise, if you're trying to copy strings to them, you'll need strcpy or its kin.

    Also, you need to either specify a size for your array (the member of the structure), or you need to get rid of it, and use a pointer to a char instead.

    Finally...
    Quote Originally Posted by esbo
    I have been programming for years, but I started at the shallow end!!

    And I would never attempt anything as monsterous as that today!!
    You're a liar, and are too ........ing stupid to know how to simply initialize an array, and know no C at all. Get off the forum, I'm tired of your lies and misinformation crapping up the C board. I will endevor to get you permenantly banned every chance I get.


    Quzah.
    Hope is the first step on the road to disappointment.

  2. #17
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Oh my god, I forgot to wrap {} around the whole thing. I'm DUMB!!!

  3. #18
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You only need to wrap as I did if you're initializing structure elements. Your method using just a multidimensional array is (currently) correct. The braces around each word is because they're structure elements. Like so:
    Code:
    struct foo {
        int a;
        float b;
        char c;
        char *str;
    } arrayoffoo[] =
    {
        { 10, 1.23, 'c', "hello" },
        { 55, 3.67, 'x', "goodbye" },
        {  9, 22.0, 'y', "foo bar baz" },
    
    };
    The first set of braces, excluding the actual structure definition, wrap up the bounds of the entire array. The nested sets are for each structure. Each structure's elements are grouped by a pair of braces. When you reach the } on the inner set, you know you're at the end of the elements for that structure. When you reach the final }, you've reached the end of the array's elements.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #19
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by echo49
    esbo: i have done some other programming languages before, including C++, but i've never tried arrays of structs or classes.

    tonto:

    the first alternative, it gives the same error message.
    the second alternative, it says "expected ')' before '[' token"... does this mean there's something wrong with the array of structs itself?
    What exactly is it you are trying to do?
    Are you are trying to program something or just trying to use loads of 'things you can do'
    in C?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. allocatable arrays in CLASSes and STRUCTS
    By simone.marras in forum C++ Programming
    Replies: 4
    Last Post: 03-14-2009, 10:50 AM
  2. a question on pointers, structs and arrays
    By onefootswill in forum C Programming
    Replies: 3
    Last Post: 12-06-2007, 01:27 AM
  3. Using strcpy() and arrays of structs
    By vital101 in forum C Programming
    Replies: 3
    Last Post: 04-26-2007, 09:04 PM
  4. arrays in structs
    By *DEAD* in forum C Programming
    Replies: 3
    Last Post: 11-25-2006, 07:35 AM
  5. malloc for structs and arrays
    By rkooij in forum C Programming
    Replies: 15
    Last Post: 05-04-2006, 07:38 AM