Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: How include a large array?

Reply
Thread Tools

Re: How include a large array?

 
 
James Kuyper
Guest
Posts: n/a
 
      04-03-2013
On 04/03/2013 10:53 AM, http://www.velocityreviews.com/forums/(E-Mail Removed) 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?
> 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
easier.

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.
--
James Kuyper
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
/* #include <someyhing.h> */ => include it or do not include it?That is the question .... Andreas Bogenberger C Programming 3 02-22-2008 10:53 AM
Re: the use of #include <a_file.h> v/s #include"a_file.cpp" Elie Nader C++ 1 11-28-2003 03:12 PM
Re: the use of #include <a_file.h> v/s #include"a_file.cpp" Rolf Magnus C++ 2 11-28-2003 12:26 PM
#include "bar" negates #include <string> ; how to fix? Danny Anderson C++ 5 08-15-2003 06:38 PM



Advertisments