trying to initialize an array of structures

This is a discussion on trying to initialize an array of structures within the C Programming forums, part of the General Programming Boards category; Hi. Simple quesiton, I think: I'm trying to initialize an array of structures, each with an integer and 4 integer ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    3

    trying to initialize an array of structures

    Hi. Simple quesiton, I think: I'm trying to initialize an array of structures, each with an integer and 4 integer pointers. I have written the following:

    Code:
    main(){
        struct lattice_site{
             int spin;
             int *right_pointer;
             int *left_pointer;
             int *up_pointer;
             int *down_pointer;
        };
        struct lattice_site mc_array[2];
        mc_array[0] = {1,0,0,0,0};
        return 0;
    }
    The compiler gives me the following error:
    structure_tester.c: In function `main':
    structure_tester.c:15: parse error before '{' token

    line 15 refers to the line " mc_array[0] = {1,0,0,0,0}; "

    What confuses me is that this syntax works when you aren't using an array of pointers. That is, if I replace mc_array[i] with mc_array in both places, it initializes just fine.

    I really need to make a 2-dimensional array of structures like the one above. Will this work?

    Thanks!
    Last edited by dreamgoat; 09-26-2004 at 04:05 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    This is assignment,
    Code:
    struct lattice_site mc_array[2];
    mc_array[0] = {1,0,0,0,0};
    This is initialisation
    Code:
    struct lattice_site mc_array[2] = {
        {1,0,0,0,0}
    };
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    3
    Thanks, that second one works. I'm not sure how to loop it, though, since I want to initialize a bunch of these things at once. Is there another way to write it that lends itself to this context?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    Then you're going to have to do things like
    Code:
    mc_array[i].spin = 1;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    You can set a struct equal to another one so you can set up a "dummy" struct instance and then loop...
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      struct lattice_site
      {
        int spin;
        int *right_pointer;
        int *left_pointer;
        int *up_pointer;
        int *down_pointer;
      };
      struct lattice_site dummy = {1,0,0,0,0};
      struct lattice_site mc_array[2];
      int i;
    
      for(i = 0;i < 2;++i)
        mc_array[i] = dummy;
    
      return 0;
    }
    This might be a gcc extension, but I don't think it is...someone please correct me if I'm wrong as I don't want to be suggesting non-portable solutions.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Initializing array of structures causing bus error
    By Aaron M in forum C Programming
    Replies: 5
    Last Post: 03-05-2006, 01:40 AM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  3. filling an array of structures?
    By voodoo3182 in forum C Programming
    Replies: 9
    Last Post: 08-06-2005, 06:29 PM
  4. Replies: 6
    Last Post: 02-15-2005, 11:20 PM
  5. Need serious help on array of structures
    By cwd in forum C Programming
    Replies: 2
    Last Post: 11-11-2001, 03:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21