Initializing arrays, an advise is needed.

Phil Howard KA9WGN phil at ux1.cso.uiuc.edu
Wed May 8 09:46:00 AEST 1991


abed at saturn.wustl.edu (Abed M. Hammoud) writes:

>	I have a program that calls a routine multiple times while
>	running. Every time the routine get called it have to calculate
>	samples of a function say sin(x) and fill an array of like
>	n values long. where n, is the same during a given run.

>	My question is, is there a way to initialize the elements of
>	the array in something like

>	float a[n] = { sin(a0), sin(a1), ....., sin(a(n-1)) }

I assume from one run to the next, n can be different, and n is also passed
as part of the arguments to your function anyway.  I also assume n == 0 is
not a valid condition.

Define a static integer internal to the subroutine and initialize it to 0.
I'll refer to it as k.

When the subroutine is entered, compare the incoming n to k.  If they are
the same, go on... the table is initialized.

If they are not the same, assert that k == 0 && n != 0, and if ok, execute
the code to initialize the table and fall through to do the deed for this
first function call as above.
-- 
 /***************************************************************************\
/ Phil Howard -- KA9WGN -- phil at ux1.cso.uiuc.edu   |  Guns don't aim guns at  \
\ Lietuva laisva -- Brivu Latviju -- Eesti vabaks  |  people; CRIMINALS do!!  /
 \***************************************************************************/



More information about the Comp.lang.c mailing list