Thread: How do I declare an array of structures?

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    78

    How do I declare an array of structures?

    I am trying to declare a global array of structures, I have tried placing it in my main, and tried placing it in my header, but it never seems to work regardless of where I define it. Currently, I have it defined in my header:

    Code:
    struct boardstate{int x;};
    typedef struct boardstate state[1000];
    In a .c file I then try to access .x:
    Code:
    state[0].x=0;
    This gives me an "parse error before '[' token" error. I've also tried defining the structure in my main, and then listing extern struct boardstate state[1000]; in my header, but that doesn't work either.
    Last edited by Adam Rinkleff; 06-24-2011 at 03:30 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why are you trying to declare a new type as an array? Just do:
    Code:
    typedef struct boardstate bs;
    Then make an array of them:
    Code:
    bs states[ 1000 ];
    Anyway, creating a typedef doesn't automatically create an instance of that type. With what you have, you would need:
    Code:
    state somestates;

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

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    typedef doesn't declare/define a variable, it provides an alias for a type. You declared a type, called state, which is an array of 1000 struct boardstates. You would make a variable likes so:
    Code:
    state array_of_1000_boardstates;
    array_of_1000_boardstates[0].x = 42;
    I generally avoid embedding pointers or arrays in typedefs. What I suspect you wanted is something like:
    Code:
    struct boardstate {int x;};
    // don't define it here, global variables are icky
    
    int main(void)
    {
        struct boardstate array_of_structs[1000];
        array_of_structs[0].x = 42;
        return 0;
    }
    EDIT: Dang you Quzah! You're too fast.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    First declare a single structure and get that working.
    Once that works, put square brackets after the variable name, with a number between them.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    I have no idea what typedef is, or why i have to declare "bs" between boardstate and state.
    All I want is a global array of 1000 structures each containing variable x that I can access from any file of my program. I just don't know the correct syntax or where to declare it.
    I already have a 'single structure' working. Its easy to make a structure in my main and use it. What I want is an array of 1000 of them that are defined globally. That's what I don't know how to do.

    Ok so now I have this in my header.

    Code:
    struct boardstate{int x}; typedef struct boardstate bs; bs state[1000];
    but in my .c file I get "request for member in something not a structure" when I try to use
    Code:
     state[0].x=0;
    Last edited by Adam Rinkleff; 06-24-2011 at 03:41 PM.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Did you actually include your header?
    Code:
    typedef struct boardstate
    {
        int x;
    } bs;
    int main( void )
    {
        bs states[ 10 ];
        states[ 0 ].x = 42;
        return 0;
    }
    When in doubt, make a small self-contained program to test.


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

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    Yes, my header is included. The problem is my syntax. I still don't know the correct syntax to globally declare an array of structures that can be read by multiple files. It doesn't help to suggest
    Code:
    typedef struct boardstate
    {
        int x;
    } bs;
    int main( void )
    {
        bs states[ 10 ];
        states[ 0 ].x = 42;
        return 0;
    }
    because although your structure is global, states[10] is local and cannot be read outside of main() unless the header is used, and I don't know what to put in the header. That's really what I'm asking here. Do I use extern? Followed by what? Do I declare the structure in the header? I just don't know. Please pretend you have another.c file which needs to access states[10];, outside of main(). How do you declare the array so that the other .c file can access it?
    Last edited by Adam Rinkleff; 06-24-2011 at 03:49 PM.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Adam Rinkleff View Post
    I have no idea what typedef is
    So don't use it then. Would you let a small child pick up a firearm when they don't know what it is or how to use it? No, because they might hurt/kill somebody. You're hurting/killing your program when you use stuff you don't understand. So go learn about it. You seem to have the syntax down if nothing else. Just read up on what it does, since you didn't get my alias comment in my previous post.

    or why i have to declare "bs" between boardstate and state.
    You don't really have to, but you have to admit, it's easier to keep typing "bs" than it is "struct boardstate". That's your typedef at work, making "bs" an alias for the more cumbersome "struct boardstate".

    All I want is a global array of 1000 structures each containing variable x that I can access from any file of my program. I just don't know the correct syntax or where to declare it.
    The code you posted has the correct declaration. As Quzah suggested, you probably didn't #include your header file in your .c file. Also, it's generally a bad idea to define a variable (actually reserve space for it) in the header file since it can cause problems when using multiple .c files. Move the following line into your .c file:
    Code:
    bs state[1000];
    EDIT:
    If you want this variable used in multiple .c files, you need to make an extern declaration (just describe the variable, don't reserve space) in the header file, like so:
    Code:
    extern bs state[1000];
    Last edited by anduril462; 06-24-2011 at 03:58 PM.

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    Quote Originally Posted by anduril462 View Post
    Move the following line into your .c file:
    Code:
    bs state[1000];
    I'm trying to declare a =global= state[1000]; If I move it into my .c file, its going to be local and not global, am I wrong?? If you don't think I should use typedef, great, I have no idea - please give me an example without it. If I don't have to use bs, then please give me an example without it. That's all I'm asking: I want to declare a global array of structs that can be accessed by any .c file in my program. Although I appreciate your assistance, as far as I can tell none of the examples given here will actually create a global array of structs that is accessible from any .c file in the program. I know how to make a global array of structs when I have one file, but I have thirty files. I assume I have to do something with the header. What, exactly? Where do I declare the structure? If I don't need typedef or bs, then what is the -simplest- most basic way to define it? Do I need to use extern in the header? What is the syntax?
    Last edited by Adam Rinkleff; 06-24-2011 at 03:59 PM.

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Yeah, sorry, I hit "post" too soon. Read my edit in my last post. It explains what you need in your header file. You only want to define a variable (actually create it) in one .c file. You put the declaration (describe the type, how many) in the header file with the "extern" keyword, and #include that header wherever you need to access the global. The extern keyword says "here's what it looks like, but the definition is external, in another translation unit (basically a fancy word for a .c file).

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    Quote Originally Posted by iMalc View Post
    First declare a single structure and get that working.
    Once that works, put square brackets after the variable name, with a number between them.
    I need it to be global. That's what I'm really asking here. How to make it global, and global over multiple files.

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    I've also tried declaring the structure above my main():
    Code:
    struct boardstate{
    	int x
    }; typedef struct boardstate state[1000];
    and then putting in my header
    Code:
    extern state[1000];
    but that gives me "state redeclared as different kind of symbol"

    and so then I tried in my header
    Code:
     extern boardstate state[1000];
    but that gives me a parse error before "state"

  13. #13
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Adam Rinkleff View Post
    I need it to be global. That's what I'm really asking here. How to make it global, and global over multiple files.
    We get that! We know what you want, and we're telling you how. What don't you get about global? If it's not in a function (if it's not local), it's global. You define the variable in one .c file only. Don't put the definition inside a function, and you will have a global variable. You declare that variable as extern in the header file. You #include that header file in every .c file that needs to know about the global variable.

  14. #14
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Look at what I told you to put:
    Code:
    extern bs state[1000];

    Look at what you put:
    Code:
    extern state[1000];
    Why did you take the type away. You need to put a "bs" in between extern and state.

  15. #15
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    Quote Originally Posted by anduril462 View Post
    You declare that variable as extern in the header file.
    Yes... but... how? It'd be a lot simpler if you'd just try it yourself, and then post the code. Telling me that it needs to be declared as an extern doesn't help at all. I already know that. I don't know the syntax.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 02-12-2011, 10:50 AM
  2. Replies: 7
    Last Post: 12-30-2009, 03:02 PM
  3. Declare an array in the BSS segment
    By ^xor in forum C Programming
    Replies: 1
    Last Post: 05-27-2005, 05:12 PM
  4. Replies: 4
    Last Post: 02-02-2003, 05:45 AM
  5. How to declare this array?
    By ooosawaddee3 in forum C++ Programming
    Replies: 2
    Last Post: 10-13-2002, 01:51 PM