Velocity Reviews

Velocity Reviews (
-   C Programming (
-   -   Re: How include a large array? (

Eric Sosman 04-03-2013 06:32 PM

Re: How include a large array?
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;

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

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

All times are GMT. The time now is 08:10 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.