Thread: Correctly defining and declaring arrays (in header and source files)

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    73

    Correctly defining and declaring arrays (in header and source files)

    This one is easy for you experts

    I want to start and organize my code better, even for small programs.
    And while I got the basics from reading info from around the net,
    There are still some questions left open.

    1. Global array should be declared In a header file as extern right ..?
    But should I declare it as:
    Code:
    extern int array[]; // without the actual size
    or like this:
    Code:
    extern int array[3];
    And as I understand I should define it only once In one of my .c files and I can then reference it from all the c. files in the program ?

    EDIT: It seems a little redundant to me to write (almost) the same thing twice: one time in the header and second time in the .c file, althouh the second time is the definition.

    2.What if I don't declare the array as extern ? than I need to define it in every .c file that I want to reference it ? Is this good for keeping variables private to a certain c file ? and if not...what is the preferred way to declare a private variable ?

    Thx.
    Last edited by patishi; 08-01-2014 at 02:57 PM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    A global array does not have to be declared in a header file. However, doing so allows the array to be used, without having to copy/paste the declaration to everywhere it is needed. As soon as there are declarations that are copy/pasted multiple times, there is an opportunity to get things wrong (e.g. have to declarations that are not equivalent). Header files allow avoidance of copy/paste.

    As to what I would do, it depends on whether the array is being initialised at the point of definition, and whether code that uses the array needs to know its actual size.

    If it is NOT being initialised at the point of definition (in other words the size has to be specified overtly) then I would do this.
    Code:
    /*  in header */
    #define ARRAY_SIZE 42
    extern int array[ARRAY_SIZE];
    
    /*   in one and only one source file which has #include'd the header */.
    int array[ARRAY_SIZE];
    If it is being initialised when it is defined and other code needs to be able to directly access the array size, then I would do this;
    Code:
    /*  in header */
    extern int array[];
    extern int array_size;
    
    /*   in one and only one source file which has #include'd the header */.
    int array[] = {1,2,3,4,5};
    int array_size = sizeof(array)/sizeof(*array);
    
    /*   in code that uses the array and has #include'd the header */
    
    int main()
    {
         for (i = 0; i < array_size; ++i)
         {
               /*  do something with array[i] */
         }
    }
    (I'd probably specify that array_size be of type size_t, not int, but you get the idea).

    If the array is being initialised when being defined, but code which uses the array does NOT need to know the array size (for example, a sentinel value is used to mark the last element) then I wouldn't supply the size. For example;
    Code:
    /*  in header */
    extern int array[];
    extern int sentinel;
    
    /*   in one and only one source file which has #include'd the header */.
    int array[] = {1,2,3,4,5, -1};
    int sentinel = -1;
    
    /*   in code that uses the array and has #include'd the header */
    
    int main()
    {
         for (i = 0; array[i] != sentinel; ++i)
         {
               /*  do something with array[i] */
         }
    }


    As to your second question, the whole point of extern is allowing a SINGLE definition for an array that is shared between compilation units. If the array is not declared extern (and defined once) then EVERY compilation unit which defines the array has its own local instance of the array. Changes made to one array, in one compilation unit, do not affect the array defined in another compilation unit.

    If you really want to confuse yourself, declare the array extern in some compilation units but not in others. The ones that have declared it extern will share one array among themselves. The ones that don't have an extern declaration will each have a local instance of an array with that name. The two don't mix well.
    Last edited by grumpy; 08-01-2014 at 03:52 PM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by patishi View Post
    I want to start and organize my code better, even for small programs.
    In this case stop using global vars
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Jun 2014
    Posts
    79
    Grumpy, I LOVE your reply! It's bundled with so many details I was not aware of (though I read a lot about the C language) that I took note of your precious words and I'm surely going to use them as a reference for my future "programs" (uh... pretentious definition, isn't it?). Some tons of thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confusion on header and source files
    By dnguyen1022 in forum C++ Programming
    Replies: 4
    Last Post: 01-17-2009, 03:42 AM
  2. compiling source files and header file at once?
    By 1jackjack in forum C Programming
    Replies: 10
    Last Post: 05-04-2007, 11:06 AM
  3. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  4. header and source files
    By gtriarhos in forum C Programming
    Replies: 3
    Last Post: 10-02-2005, 03:16 AM
  5. windows header files not linking correctly
    By skorman00 in forum Windows Programming
    Replies: 2
    Last Post: 04-13-2005, 11:14 AM