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. Advertising

  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. Advertising

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
    Replies:
    3
    Views:
    849
    Andreas Bogenberger
    Feb 22, 2008
  2. Ketchup
    Replies:
    1
    Views:
    227
    Jan Tielens
    May 25, 2004
  3. Siri Cruise

    Re: How include a large array?

    Siri Cruise, Apr 3, 2013, in forum: C Programming
    Replies:
    0
    Views:
    212
    Siri Cruise
    Apr 3, 2013
  4. Joe Pfeiffer

    Re: How include a large array?

    Joe Pfeiffer, Apr 3, 2013, in forum: C Programming
    Replies:
    0
    Views:
    222
    Joe Pfeiffer
    Apr 3, 2013
  5. James Kuyper

    Re: How include a large array?

    James Kuyper, Apr 3, 2013, in forum: C Programming
    Replies:
    0
    Views:
    257
    James Kuyper
    Apr 3, 2013
Loading...

Share This Page