Hey, will this guarantee all the elements in my array will be declared zero? or should I got for a for loop instead?
Code:int array[size] = {0};
Hey, will this guarantee all the elements in my array will be declared zero? or should I got for a for loop instead?
Code:int array[size] = {0};
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Don't forget to compile as C99 if you're going to use variable-length arrays. Alternatively, you could use calloc to allocate the memory for array and initialize it to 0s.
Really? That would seem to only set one if you use size as an index. The most common way I have seen to init an array (to zero for instance) is this:
Code:int myArray[size]; memset(myArray, 0, sizeof(int)*size);
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Interesting; changing your initializer to 1:
Produces:Code:int myArray[10] = {1}; for(int x = 0; x < 10; x++) printf("myArray[%d] = %d\n", x, myArray[x] );
On GCC in debug mode (which explains the other 0's). I would expect it to be garbage in release mode. The {} are only specifying one element. Is it possible that you are also running in debug mode (hence all vars are initialized, at least to zero) and being fooled by the output?Code:myArray[0] = 1 myArray[1] = 0 myArray[2] = 0 myArray[3] = 0 myArray[4] = 0 myArray[5] = 0 myArray[6] = 0 myArray[7] = 0 myArray[8] = 0 myArray[9] = 0
J.
Last edited by jeffcobb; 12-14-2009 at 02:16 PM. Reason: Forgot closing paren
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Note: more portable than using memset as earlier mentioned.
If you specify anything other than 0, when you initialize then the first index is only initialized all others will be set to 0.Code:static int myArray[10];
After playing with this a bit more it seems that {0} does work, with the caveats being you can only use 0 and only at array instantiation. Any other value at any other time will require stronger measures such if you need to *re*set an array or defer initialization or use a default value other than zero.
But if you need to init the array once only and to zero only, this would seem to be an acceptable solution ^__^
I was just flipping thru the new standard to find out if this is guaranteed. It is:
Which means:Originally Posted by 6.7.8 1682
That would explain why you can initialize the first few (or one) item to anything, and then everything else is zero, as jeffcobb points out.1653 If an object that has static storage duration is not initialized explicitly, then:
1654 — if it has pointer type, it is initialized to a null pointer;
1655 — if it has arithmetic type, it is initialized to (positive or unsigned) zero;
1656 — if it is an aggregate, every member is initialized (recursively) according to these rules;
1657 — if it is a union, the first named member is initialized (recursively) according to these rules.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
This would also be useful in resource-constrained embedded environments too....will keep it in mind.