CBFalconer said:
Eric said:
assiss wrote:
int *list[6] = {NULL};
list[0]=NULL, then the other 5 elements will be initialized with 0.
ONLY if you are sure that NULL equals to 0.
I am sure that NULL equals 0. Always. On every
Standard-conforming C implementation ever built. Amen.
Oh? Consider:
#include <stdio.h>
int main(void);
int n = 0;
if (n == NULL) puts('You are right');
else puts('You are wrong');
return 0;
}
Note that I did NOT write "if (0 == NULL)"
... and I for my part did NOT write "NULL equals zero."
(Also, it looks like your double-quote key is functioning
erratically: Sometimes it omits the left or right half -- hard
to tell which -- of its symbol. Have it checked before it gets
even worse, and maybe catches fire. ;-)
Back to the thread: assiss, Chuck and I have been having a
little fun at your expense, and I apologize. Here's the scoop:
Every variable that's "initialized in part" is "initialized in
full." If you provide an initializer for just a few elements
of an array or struct, all the other elements are initialized
for you. The elements you don't initialize explicitly are all
initialized to "zeroes of the proper type:" 0.0 for double,
'\0' for char, (char*)0 for char* pointers, and so on. (Union
objects are a little different because they can hold only one
value at a time, so when a union is initialized this way what
happens is that its first element is given a zero of the type
appropriate to that element.) This all works even on machines
where the various kinds of zero and NULL might not be represented
as all-bits-zero; it's the compiler's business to make it work.
What variables are initialized this way? Two kinds: those
with static storage duration (variables outside functions plus
`static' variables inside functions), and any variables for
which you provide a partial initializer:
int a; /* a == 0 */
int b = 3; /* b == 3 */
int c[4] = {1,2}; /* c == 1,2,0,0 */
struct s {
int x, y;
} d = { 1 }; /* d.x == 1, d.y == 0 */
union u {
char *cp;
int i;
float f;
} e; /* e.cp == (char*)0 */
void f(void) {
static int fa; /* fa == 0 */
int fc[4] = {9}; /* fc == 9,0,0,0 */
struct s fd = { 2 }; /* fd.x == 2, fd.y == 0 */
int v; /* NOT INITIALIZED */
}
The newer "C99" version of the Standard allows some fancier
forms of initializers (for example, you can provide explicit
initializers for elements [5],[1],[9] of an array, in that order),
but even then the rule holds: Initialize any part of something,
and all the parts you don't initialize receive zeroes.