TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: All
from: Kurt Kuzba
date: 2004-02-15 07:10:24
subject: [C] Question about external variable initialization

From: "Kurt Kuzba" 


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

    Try moving it to another .c module and using #include.  If you
 are using an advanced programming environment, do NOT allow the
 automatic inclusion of the external file.  If you DO allow the IDE
 to automatically include the external file, then you must omit the
 #include statement.  Here, I created a tiny function which defines
 a static string array and does nothing other than return a pointer
 to that string array.  This was tested under VCC 5.0.  Remember
 that such arrays are not usually available for redefinition,
 though.  You could also define a dynamic array and use the external
 module to populate it.  This leaves you room for user defined data.
 (Unnecessarily detailed, I know.  I'm not talking down to you, but
 it's a public forum and there may be beginners reading this.)

/*_|_|  initstr.c  PUBLIC DOMAIN by Kurt Kuzba  (04/15/2004)
_|_|_|  External string array definition .c module. _|_|_|  No guarantee or
warantee of suitability for usage is _|_|_|  either given or implied.
_|_|*/
char** bannerarray()
{
    static char *banners[] = {
        "Banner number one.",
        "Banner number two.",
        "Banner number three.",
        "Banner number four.",
        "Banner number five.",
    };
    return banners;
}
/*_|_|  end xtrn_arr.c
_|_|*/

/*_|_|  xtrn_arr.c  PUBLIC DOMAIN by Kurt Kuzba  (04/15/2004)
_|_|_|  Define string arrays in external modules. _|_|_|  No guarantee or
warantee of suitability for usage is _|_|_|  either given or implied.
_|_|*/
#include 
#include "initstr.c"
char** Banners;
int main(void)
{
    int iterate;
    Banners = bannerarray();
    for(iterate = 0; iterate < 5; iterate++)
        puts(Banners[iterate]);
    return 0;
}
/*_|_|  end xtrn_arr.c
_|_|*/

>  kkuzba{at}centurytel.net   http://home.centurytel.net/kkuzba
>  Do your work, then step back.  The only path to serenity.
>  ( Tao te Ching - verse 9 )

--- 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™.