Thread: Declaring and initialising constant nested arrays

  1. #1
    Registered User officedog's Avatar
    Join Date
    Oct 2008
    Posts
    77

    Declaring and initialising constant nested arrays

    I'm having a little bit of bother trying to understand the way to declare global variables (infact I'm not sure if this is really the best way to go... so alternative suggestions are also very welcome)

    My current project is to try to develop a GUI button and am writing it using a combination of C and openGL. I would like to have two arrays - one which holds the coordinates of the button and one which holds the colours for each vertex.

    I created a header and source file for the 'button code'. I began by creating a struct to hold various data associated with the button including a label coords[8][2] and colours[8][3]. And also in the header I declared a function which would initialise the struct.

    The issue I am struggling with is where to put the data itself!

    If I declare the core data for say the button coordinates in the button.h file, there is no problem, but as soon as I also try to initialise the nested array... eg.

    buttonData[8][2] = {[0,0], [1,0].... etc}

    I get compiler errors. I get compiler (syntax) errors if it is a 'flat' array too, such as

    buttonData[16] = {0,0,1,0.... etc}

    In the end, the only solution I could find with my current understanding is to declare and initialise 1 dimensional arrays in the main.c file, above the main() function and use the 'extern' declaration in my button.h file. This works fine, but is not quite the way I wanted to go as I was hoping to have everything in the button.h and button.c files

    Am I thinking about this all wrong?

    Thanks in advance for any help with this

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    You can only initialize arrays with initialization lists when you're declaring the arrays.
    Code:
    int buttonData[8][2] = {{0,0}, {1,0}.... etc}
    int buttonData[16] = {0,0,1,0.... etc}
    Last edited by robwhit; 10-28-2008 at 09:28 AM. Reason: pete is on the prowl...

  3. #3
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    You're missing an opening curly brace on your first line.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  4. #4
    Registered User officedog's Avatar
    Join Date
    Oct 2008
    Posts
    77
    I can see the light now! I tried this and it works fine. Thank you very much for the quick replies.

    One last related question if that's OK....

    If I declare and initialise the array in the header file (button.h) I get a compiler error saying something about a duplicate symbol (I'm assuming this is something about the preprocessor copying and pasting button.h into both button.c and main.c). However, if I declare and initialise them in the button.c file it works just fine.

    Is there a general rule that I should follow here... i.e. is the rule that one should not (or cannot?) initialise variables in the header file, only in the .c file?

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You should not initialize data in header files. It will not lead to anything good EVER.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    /* header */
    extern int array[dim1][dim2];
    
    /* code */
    int array[dim1][dim2] = etc;

  7. #7
    Registered User officedog's Avatar
    Join Date
    Oct 2008
    Posts
    77
    This is very helpful. Many thanks for the replies.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vectors, declaring and initialising
    By r0bbb in forum C++ Programming
    Replies: 2
    Last Post: 03-27-2005, 09:30 AM