| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | [C] Question about external variable initialization |
From: "Len Philpot"
Hi all,
I'm a lurker here (as on c.l.c. and years ago, on C_ECHO) and a perpetual*
newbie at C... A nearly 45-year-old newbie, that is :-)
I've been trying to get external variable usage understood and wrote a
simple 'learning' program, consisting of the following files :
-+- ext.c ----------------------------------------------------------
#include
#include "ext.h"
int main(void)
{
/* this is a definition (and hence, a declaration as well) */
int i;
/* this is the initialization of ext_int, already defined
and declared in ext.h */
ext_int = 5;
/* ...and likewise for ext_char_arr[] */
ext_char_arr[0] = "element zero";
ext_char_arr[1] = "element one";
ext_char_arr[2] = "element two";
printf("ext_int : %d\n", ext_int);
printf("ext_char : %s\n", ext_char);
for(i = 0; i < 3; i++)
printf("ext_char_arr[%d] : %s\n", i, ext_char_arr[i]);
return 0;
}
-+- ext2.c ---------------------------------------------------------
/* these 3 are definitions, the second is also an initialization */
int ext_int;
char ext_char[] = "zoom";
char *ext_char_arr[3];
-+- ext.h ----------------------------------------------------------
/* these three are declarations of externally defined variables */
extern int ext_int;
extern char ext_char[];
extern char *ext_char_arr[];
The program compiles clean and works as expected (and I /think/ my comments
are correct), but I still haven't achieved what I want - To move the
initialization of the elements of ext_char_arr[] out into its own file, or
at least out of the main() file. I get various redeclaration errors and
such from gcc and bcc when I simply put the declarations into ext2.c. I've
always understood (and I use that word lightly ) that executable
code shouldn't go in header files, so I've not even attempted to put it
there.
I strongly suspect that the problem here arises from a thorough lack of
understanding on my part, but I'm not sure where (possibly more in relation
to the nature of character arrays than scope?).
What precipitated this was a workalike of the 'banner' program I wrote
(called "pennant", naturally). The program is about 50 lines of
code and 800+ of the large "character" definitions. Obviously it
would be nice to get those definitions in their own file.
* I say perpetual, since when I find time to work on C, I generally
spend most of my time relearning what I barely knew from the last time I
spent any time on C (typically months to years earlier). Despite working
on/with Solaris all day, I have basically no opportunity to do any
programming, so anything like this gets shoehorned in between work, life,
wife and 5 year old daughter... :-)
Thanks for any clarification...
--
-- Len Philpot -> len{at}philpot.org <--
------ > -----> http://philpot.org/ <--
--- BBBS/LiI v4.01 Flag-5
* Origin: Prism's_Point (1:261/38.1)SEEN-BY: 633/267 270 @PATH: 261/38 123/500 106/2000 633/267 |
|
| SOURCE: echomail via fidonet.ozzmosis.com | |
Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.