Re: How include a large array?

Discussion in 'C Programming' started by Eric Sosman, Apr 3, 2013.

  1. Eric Sosman

    Eric Sosman Guest

    On 4/3/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?


    It gave you "an error" because there was "something wrong."
    (There! Wasn't that easy?)

    > 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?


    When you #include a file, the effect is almost exactly[*]
    as if you had copied and pasted the file's content in place of
    the #include directive. That is, any stream of source tokens
    you could have written in the file could just as well have been
    in an #include'd file. Declarations? Sure, #include them.
    Definitions? Instances? Macro definitions? #include them,
    too. Incomplete fragments of statements? Go ahead, #include
    whatever you please: Put `int' and `main' and `(' and `void'
    and ')' in five different files, and #include them (in the
    proper order) to start defining your main() function.

    It is "customary" to use #include files for declarations
    and macro definitions and the like, and that's by far their
    most common use. But in fact it's entirely possible to bring
    other kinds of stuff into the compilation by way of #include.

    [*] I've thought of a few differences: The __FILE__ and
    __LINE__ macros will expand differently in #include'd material
    than if that material were incorporated bodily. You can't begin
    a comment or token inside an #include'd file and finish it after
    the #include, nor vice versa. Some recursive inclusions, like

    /* a.h */
    #ifndef H_A
    #define H_A
    #include "b.h"
    extern int a;
    #endif

    /* b.h */
    #ifndef H_B
    #define H_B
    #include "a.h"
    extern int b;
    #endif

    .... cannot be reproduced by copy-and-paste. And there may be
    other differences I've overlooked. But by and large, #include
    can be thought of as "Everything THERE appears HERE," so
    anything that would be legal HERE could equally well be THERE.

    --
    Eric Sosman
    d
    Eric Sosman, Apr 3, 2013
    #1
    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:
    884
    Andreas Bogenberger
    Feb 22, 2008
  2. Ketchup
    Replies:
    1
    Views:
    233
    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:
    218
    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:
    228
    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:
    259
    James Kuyper
    Apr 3, 2013
Loading...

Share This Page