Path: utzoo!utgpu!water!watmath!clyde!rutgers!cmcl2!husc6!sri-unix!quintus!sun!pitstop!sundc!seismo!uunet!mcvax!diku!dde!be From: be@dde.uucp (Bjorn Engsig) Newsgroups: comp.lang.c Subject: Re: initializing Summary: arrays, struct, extern Message-ID: <303@Aragorn.dde.uucp> Date: 18 Jan 88 10:18:16 GMT Organization: Dansk Data Elektronik A/S, Herlev, Denmark Lines: 37 In article <11276@brl-adm.ARPA>, PEPRBV%CFAAMP.BITNET@husc6.harvard.EDU (Bob Babcock) writes: > If I want to initialize a global variable in an include file, and have ... > But this doesn't work for an array, and I can't figure out anything > which does. Is there some trick I'm missing? I'v always used something like the following in a header file: #ifdef MAIN_PROGRAM # define EXTERN # define INITVAL(x) {x} #else # define EXTERN extern # define INITVAL(x) #endif EXTERN int variable INITVAL(27); EXTERN int array[] #ifdef MAIN_PROGRAM {1, 2, 3, 4, 5, } #endif ; BTW, gwyn@brl-smoke.ARPA (Doug Gwyn @ Ballistic Research Lab (BRL), APG, MD.) writes: > Supply static initializers in some place that makes sense, > for example in the source code for the module that owns the data. This is good for statics but NOT for externals. You really want to write the external variables ONE and only ONE place, and the header file above is very appropriate. The scheme has at least proven to be useful in the 3000 (three thousand, maybe more) source files large software product I'm working on. -- Bjorn Engsig, E-mail: ..!uunet!mcvax!diku!dde!be or be@dde.uucp -- Hofstadters Law: It always take longer than you expect, even if you take into account Hofstadters Law.