Thread: Filling an array in a structure

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    4

    Filling an array in a structure

    This is probally a newbie question (shocking becuase im a newbie) but ive looked in my C book, with google, everything, and I can't find it.

    My question is, how do you fill an array thats in a structure? in other words, i have a structure that i pass through all my functions, and in that structure is an array (int teamstats[5][18).
    I want to fill that array on the load of the program with the following data:

    int teamstats[][18] = {
    {99 ,97, 85, 82, 61, 75, 72, 87, 93, 87, 99, 26, 87, 87, 86, 85, 87, 68},

    {94, 96, 84, 81, 65, 85, 28, 86, 85, 97, 87, 94, 78, 95, 76, 90, 87, 68},

    {93, 95, 83, 80, 60, 87, 68, 97, 95, 75, 86, 89, 96, 78, 85, 78, 87, 68},

    {99, 99, 88, 77, 66, 96, 93, 64, 75, 86, 97, 86, 82, 89, 76, 81, 87, 68}
    } ;

    but I found i can't declare it like you can with a normal array (aka teamstats[][18] = {"5", "43", "34"....etc.

    im at a lost, any help would be greatly appreciated.
    thanks!

    Code:
    
    struct var 
    {
    int teamstats[5][18];
    };
    
    
    void fill(struct var *);
    
    
    void main(void)
    {
    struct var global;
    
    flil(&global);
    }
    
    void fill(struct var *global)
    {
    
    /* put code here to fill array with this data:
    
    {99 ,97, 85, 82, 61, 75, 72, 87, 93, 87, 99, 26, 87, 87, 86, 85, 87, 68}, 
    
    {94, 96, 84, 81, 65, 85, 28, 86, 85, 97, 87, 94, 78, 95, 76, 90, 87, 68}, 
    
    {93, 95, 83, 80, 60, 87, 68, 97, 95, 75, 86, 89, 96, 78, 85, 78, 87, 68}, 
    
    {99, 99, 88, 77, 66, 96, 93, 64, 75, 86, 97, 86, 82, 89, 76, 81, 87, 68}
    
    */
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can only initialize an array like that (using the braces) at the time the variable is created, on the same line the variable is created on. IE:
    Code:
    struct mystruct
    {
        char c;
        int array[3];
    };
    
    Then you'd do:
    
    struct mystruct x = { 'a', { 1, 2, 3 } };
    Or, if you're assigning them individually:

    x.array[1] = 23445;

    Through a pointer:

    struct mystruct *ptr;
    ptr = &x;

    prt->array[0] = 30;


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 06-07-2006, 09:42 AM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM