TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: Len Philpot
from: Bo Simonsen
date: 2004-01-25 13:27:46
subject: [C] Question about external variable initialization

Hi Len, and welcome aboard. :)

 LP> I've been trying to get external variable usage 
 LP> understood and wrote a simple 'learning' program, 
 LP> consisting of the following files :

Yes okay.

 LP> -+- ext.c ----------------------------------------------------------
 LP> #include 
 LP> #include "ext.h"

 LP> int main(void)
 LP> {
 LP>         /* this is a definition (and hence, a declaration as well) */
 LP>         int i;

 LP>         /* this is the initialization of ext_int, already defined
 LP>                 and declared in ext.h */
 LP>         ext_int = 5;

 LP>         /* ...and likewise for ext_char_arr[] */
 LP>         ext_char_arr[0] = "element zero";
 LP>         ext_char_arr[1] = "element one";
 LP>         ext_char_arr[2] = "element two";

Where do you malloc() those?

.. And you can't assign a string value in that way, you should use

strcpy(destination var, source var), so it should be,

strcpy(ext_char_arr[0], "element zero");

 LP>         printf("ext_int  : %d\n", ext_int);
 LP>         printf("ext_char : %s\n", ext_char);

 LP>         for(i = 0; i < 3; i++)
 LP>                 printf("ext_char_arr[%d] : %s\n", i,
ext_char_arr[i]);

 LP>         return 0;
 LP> }


 LP> -+- ext2.c ---------------------------------------------------------
 LP> /* these 3 are definitions, the second is also an initialization */
 LP> int ext_int;
 LP> char ext_char[] = "zoom";
 LP> char *ext_char_arr[3];

Would not work out with out a 

ext_char_arr[0] = malloc(SIZE in bytes);
...

In main or somewhere else, before you have to use it.

You can also use ext_char_arr[0] = strdup("This is a text"); 

It returns a pointer where strdup() did allokate memory.

 LP> -+- ext.h ----------------------------------------------------------
 LP> /* these three are declarations of externally defined variables */
 LP> extern int ext_int;
 LP> extern char ext_char[];
 LP> extern char *ext_char_arr[];

It's okay to define them external here, if we are talking about global
or static variabels. But I see you do in ext2.c ;) nevermind.

 LP> The program compiles clean and works as expected (and I /think/ my 
 LP> comments are correct), but I still haven't achieved 
 LP> what I want - To move the initialization of the 
 LP> elements of ext_char_arr[] out into its own file, or at 
 LP> least out of the main() file. I get various 
 LP> redeclaration errors and such from gcc and bcc when I 
 LP> simply put the declarations into ext2.c. I've always 
 LP> understood (and I use that word lightly ) that 
 LP> executable code shouldn't go in header files, so I've 
 LP> not even attempted to put it there.

I really doubt I did work :)

 LP> I strongly suspect that the problem here arises from a 
 LP> thorough lack of understanding on my part, but I'm not 
 LP> sure where (possibly more in relation to the nature of 
 LP> character arrays than scope?).

See my comments.

 LP> What precipitated this was a workalike of the 'banner' 
 LP> program I wrote (called "pennant", naturally). The 
 LP> program is about 50 lines of code and 800+ of the large 
 LP> "character" definitions. Obviously it would be nice to 
 LP> get those definitions in their own file.

If you have a unknown number of lines, you could use char**, or a linked
list (the linked list might be easier).

 LP> * I say perpetual, since when I find time to work on C, I generally
 LP> spend most of my time relearning what I barely knew 
 LP> from the last time I spent any time on C (typically 
 LP> months to years earlier). Despite working on/with 
 LP> Solaris all day, I have basically no opportunity to do 
 LP> any programming, so anything like this gets shoehorned 
 LP> in between work, life, wife and 5 year old daughter... 
 LP> :-)

:)

Bo



--- Maximus/UNIX 3.03b
* Origin: The Night Express - Roennede, Dk (2:236/100)
SEEN-BY: 633/267 270
@PATH: 236/100 237/9 20/11 106/1 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™.