[net.lang.c] "Re: Array Initialization

davy@ihlpf.UUCP (07/06/83)

#R:ecn-ec:15500016:ihlpf:18400001:  0:201
ihlpf!hanes    Jul  6  8:39:00 1983

In my experience, it is NEVER safe to skip initializations.  Not all language
implementations are the same, and the initialization of variables tends to be
very machine and operating system dependent.

tom@rlgvax.UUCP (07/08/83)

The "bible" (i.e. Kernighan & Ritchie's book) states quite clearly on
page 198 of the C Refernce Manual that:

	Static and externals that are not initialized are guaranteed
	to start off as 0;

There is little room for disagreement.  Until there is a C language Standard,
the K & R book is the closest to one we have.  When a Standard finally is
developed, I'll give very good odds that this requirement is in there.
In fact, every C compiler specification (contract, product requirement,
etc) that I have seen calls for implementation according to the K & R
book.

So, if it doesn't default initializations to 0, it AIN'T C.

If you are still nervous about portability (I usually am), every variant of
a C compiler that I have seen (at least 12 that I can think of offhand)
does default initialization to 0.

However, it is a good programming practice to do explicit initialization
to 0.  The reason is program legibility.  If its important that the
initial value be 0, why keep it a secret?

- Tom Beres
{seismo, allegra, mcnc}!rlgvax!tom

nather@utastro.UUCP (07/10/83)

I agree with davy that explicit initializations are wise, even when
"guaranteed" initializations are promised.  As an awk-ward example,
try the following short program on "awk" which pre-initializes every
variable, array or not:

	#! /bin/awk -f
	{
	a[i] = 12345
	printf "a[%d] = %d  ", a, i
	i = 0
	printf "a[%d] = %d\n", a, i
	}

Now, careful reading of the "awk" tutorial will tell you that "i" is
initialized to the null string, which is often zero, but not always.
(It is to printf in the above example, but *not* to the array locator).
So the above is a no-no, but less obvious than one might guess.

What is this doing in net.lang.c?  Well, I use "awk" a lot to test
out algorithms before casting them permanently into "C" -- it requires
far less overhead to get a simple program written -- but the practice
has its hazards.  

INITIALIZE!

				    Ed Nather
                             ...kpno!utastro!nather

nather@utastro.UUCP (07/10/83)

Oh, and RUN the program before you submit to the net!
In my prvious example I got the array and its index reversed in the
print statements.  Sorry.

				 We Learn by Making Mistakes...

				 Ed Nather