Re: How include a large array?

Discussion in 'C Programming' started by James Kuyper, Apr 3, 2013.

  1. James Kuyper

    James Kuyper Guest

    On 04/03/2013 10:53 AM, wrote:
    > Hi,
    > i have this problem: i want to create a large array data that can be portable and so included in multiple files. I thought about an "array.c" file that can be included in test file with an include like this:
    > #include "array.c"
    > The compiler gives me an error:why?
    > I thought about an "array.h" file, but i can't put an instance (in this case the data array) in an header file...or not?
    > How can i do?

    The C language places no special restrictions on the contents or naming
    conventions for header files. How you partition your code between the
    header files and the main program files is entirely up to you. How you
    name your header files is little more restricted - an implementation is
    allowed to impose it's own conventions on such things, and might give
    special recognition to files named with extensions of .h or .c. I
    haven't seen a compiler that refused to #include files that didn't end
    with ".h". However, I have seen text editors that went into special
    display modes for C code, but only if it occurred in a *.h or *.c file.

    However, here's some advice:
    Follow the naming conventions, even though they are just conventions. It
    doesn't cost you anything, and it will make your life as a programmer

    The purpose of a header file is to be #included into another file. But
    if you're going to do that, why not put the code from your header file
    directly into the other file, in place of the #include statement?
    There's one very good reason for doing so: if you want to include the
    same code into multiple different files. This is an excellent idea for
    typedefs, struct, union, or enumeration declarations, macro definitions,
    "extern" declarations of objects with external linkage, function
    prototypes, and inline function definitions, all of which you might want
    to be exactly the same in multiple different files. Writing them once in
    a header file, and #including them where needed, ensures that the code
    is exactly the same.

    You should NOT use a header for the definition of an array with external
    linkage. That would create multiple definitions of the array, which
    would give your program undefined behavior. Just create a *.c file
    containing the array definition, and link itin with your other source
    code files. Create a separate *.h file containing a declaration of the
    array using the 'extern' keyword. #include that header in each file that
    needs access to the array, and also in the file that defines the array
    (to ensure that the declaration of the array and the definition of the
    array are compatible with each other).

    You could use a header to create an array with internal linkage, by
    using the static keyword. However, that would create a separate copy of
    the array in each part of the program where it was #included. That's not
    a constraint violation, but it generally still not a good idea,
    particularly if it's a "large" array.
    James Kuyper
    James Kuyper, Apr 3, 2013
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Bogenberger
    Andreas Bogenberger
    Feb 22, 2008
  2. Ketchup
    Jan Tielens
    May 25, 2004
  3. Siri Cruise

    Re: How include a large array?

    Siri Cruise, Apr 3, 2013, in forum: C Programming
    Siri Cruise
    Apr 3, 2013
  4. Joe Pfeiffer

    Re: How include a large array?

    Joe Pfeiffer, Apr 3, 2013, in forum: C Programming
    Joe Pfeiffer
    Apr 3, 2013
  5. Eric Sosman

    Re: How include a large array?

    Eric Sosman, Apr 3, 2013, in forum: C Programming
    Eric Sosman
    Apr 3, 2013

Share This Page