Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Re: How include a large array? (http://www.velocityreviews.com/forums/t959360-re-how-include-a-large-array.html)

Eric Sosman 04-03-2013 06:32 PM

Re: How include a large array?
 
On 4/3/2013 10:53 AM, silusilusilu@gmail.com 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
esosman@comcast-dot-net.invalid


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.