Re: How include a large array?

Discussion in 'C Programming' started by Edward A. Falk, Apr 4, 2013.

  1. In article <>,
    <> 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?


    As others have pointed out, almost certainly it's multiple definitions
    of the same thing. We'd have to see the error message ourselves
    to be sure, though.


    Anyway, don't put the array *data* into your included file, you'll
    just replicate it among all the files that include it. Put the
    data into its own .c file (perhaps one dedicated to the purpose)
    and then declare it in a .h file:

    array.c:

    int data[] = {
    ...
    ...
    };
    int dataLen = sizeof(data)/sizeof(data[0]);


    array.h:

    #ifndef _ARRAY_H_
    #define _ARRAY_H_

    extern int data[];
    extern int dataLen;

    #endif /* _ARRAY_H_ */


    Then #include "array.h" anywhere you'll need to access that data.

    Oh, and choose better names than "data" and "dataLen", that's just asking for
    a namespace collision.

    --
    -Ed Falk,
    http://thespamdiaries.blogspot.com/
     
    Edward A. Falk, Apr 4, 2013
    #1
    1. Advertisements

  2. Edward A. Falk <> wrote:

    (snip)
    > Anyway, don't put the array *data* into your included file, you'll
    > just replicate it among all the files that include it. Put the
    > data into its own .c file (perhaps one dedicated to the purpose)
    > and then declare it in a .h file:


    > array.c:


    > int data[] = {
    > ...
    > ...
    > };
    > int dataLen = sizeof(data)/sizeof(data[0]);


    > array.h:


    > #ifndef _ARRAY_H_
    > #define _ARRAY_H_


    > extern int data[];
    > extern int dataLen;


    > #endif /* _ARRAY_H_ */


    > Then #include "array.h" anywhere you'll need to access that data.


    This reminds me of a C feature that Java also has.
    (Less obvious in Java without #include, though.)

    The list of values to initialize an array is allowed an extra
    comma after the last one. Consider initializing an array with
    #ifdef and #include:

    int data[]={
    #ifdef A
    #include "a.c"
    #endif
    #ifdef B
    #include "b.c"
    #endif
    #ifdef C
    #include "c.c"
    #endif
    }

    Now, if the data in each file ends in comma then everything works
    for any combination of #include files.

    -- glen
     
    glen herrmannsfeldt, Apr 4, 2013
    #2
    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. Danny Anderson
    Replies:
    5
    Views:
    796
    Victor Bazarov
    Aug 15, 2003
  2. Andreas Bogenberger
    Replies:
    3
    Views:
    1,290
    Andreas Bogenberger
    Feb 22, 2008
  3. Fredxx
    Replies:
    3
    Views:
    810
    Martin Thompson
    Jul 15, 2009
  4. Ketchup
    Replies:
    1
    Views:
    437
    Jan Tielens
    May 25, 2004
  5. Me Padre

    Functions in Include File - too large??

    Me Padre, Oct 7, 2003, in forum: ASP General
    Replies:
    1
    Views:
    207
    erez alster
    Oct 7, 2003
  6. Ben
    Replies:
    9
    Views:
    236
  7. Replies:
    5
    Views:
    1,718
    Xho Jingleheimerschmidt
    Apr 2, 2009
  8. Malcolm McLean

    Re: How include a large array?

    Malcolm McLean, Apr 3, 2013, in forum: C Programming
    Replies:
    6
    Views:
    340
    Eric Sosman
    Apr 4, 2013
Loading...