Re: How include a large array?
On 04/03/2013 10:53 AM, firstname.lastname@example.org wrote:
> 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?
> 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?
> How can i do?
The C language places no special restrictions on the contents or naming
conventions for header files. How you partition your code between the
header files and the main program files is entirely up to you. How you
name your header files is little more restricted - an implementation is
allowed to impose it's own conventions on such things, and might give
special recognition to files named with extensions of .h or .c. I
haven't seen a compiler that refused to #include files that didn't end
with ".h". However, I have seen text editors that went into special
display modes for C code, but only if it occurred in a *.h or *.c file.
However, here's some advice:
Follow the naming conventions, even though they are just conventions. It
doesn't cost you anything, and it will make your life as a programmer
The purpose of a header file is to be #included into another file. But
if you're going to do that, why not put the code from your header file
directly into the other file, in place of the #include statement?
There's one very good reason for doing so: if you want to include the
same code into multiple different files. This is an excellent idea for
typedefs, struct, union, or enumeration declarations, macro definitions,
"extern" declarations of objects with external linkage, function
prototypes, and inline function definitions, all of which you might want
to be exactly the same in multiple different files. Writing them once in
a header file, and #including them where needed, ensures that the code
is exactly the same.
You should NOT use a header for the definition of an array with external
linkage. That would create multiple definitions of the array, which
would give your program undefined behavior. Just create a *.c file
containing the array definition, and link itin with your other source
code files. Create a separate *.h file containing a declaration of the
array using the 'extern' keyword. #include that header in each file that
needs access to the array, and also in the file that defines the array
(to ensure that the declaration of the array and the definition of the
array are compatible with each other).
You could use a header to create an array with internal linkage, by
using the static keyword. However, that would create a separate copy of
the array in each part of the program where it was #included. That's not
a constraint violation, but it generally still not a good idea,
particularly if it's a "large" array.
|All times are GMT. The time now is 02:54 PM.|
Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.