Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Static Arrays Inside Functions Date: Sat, 27 Apr 2019 13:14:35 -0700 Organization: None to speak of Lines: 49 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="c6e41c1d9a96363646e2c0eae7343784"; logging-data="17825"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+5BgtpLUIs3TPSwvmelIpM" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:I9q7y5n3KAptT37Pyrj1b/ABtnw= sha1:Jui/mnjjh7CGiBBa+/rJkkonapE= Xref: csiph.com comp.lang.c:143498 Bart writes: > On 27/04/2019 18:52, F. Russell wrote: >> I need to include a lookup table, via an array, within a library function >> that will be linked to other C programs: >> >> double some_function(...) >> { >> static double[100] = {55.55, 66.66, ...} You forgot to provide a name for the array. static double arr[100] = {55.55, 66.66, /* ... */}; The "static" keyword means that the object is created and initialized once, during program startup. Defining it inside a function means that the name is visible only within the function, but the object exists and can be accessed indirectly from anywhere (for example if the function saves a pointer value). >> ... >> } >> >> I am not sure when this array will actually be initialized. >> >> Will the array be initialized during compile time so that the >> function code contains the values? >> >> Or will the array be initialized during the first call to the function >> and thereafter remain static? > > At compile-time. The compiler generates code that results, at run-time, in the object being allocated and initialized before main() is invoked. Typically a copy of the data will be included in the executable, loaded at program startup along with the code. > (Otherwise it wouldn't retain the same values on subsequent calls.) In principle, the compiler could create an implicit flag so that the initializion occurs exactly once, when the function is called for the first time. That would probably be conforming as long as the visible behavior is the same, but I can't think of any good reason to do it that way. -- Keith Thompson (The_Other_Keith) kst-u@mib.org Will write code for food. void Void(void) { Void(); } /* The recursive call of the void */