Is this okay? It compiles on my compiler, but of course that doesn't mean anything.
Code:int array[100] = {0,};
Is this okay? It compiles on my compiler, but of course that doesn't mean anything.
Code:int array[100] = {0,};
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Yes, as long as it is initializing an array. (Whereas {0} works for anything.) It's very much like this.
Code:int value[] = { 1, 2, 3, };
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
I saw the guy that did that example. I tried something similar on Dev-C++ and I got expected output, but I don't think it's what the guy wanted.
Output:Code:#include <stdio.h> int main() { int array[20] = {2,}; int x; for (x = 0; x < 20; x++) printf("%d \n", array[x]); return 0; }
Code:2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Last edited by SlyMaelstrom; 11-21-2005 at 01:45 PM.
Sent from my iPad®
Of course. This is in section 6.7.8 of the ISO C standard:
And of course we all know that (found in the same section of the standard):21 If there are fewer initializers in a brace-enclosed list than there are elements or members
of an aggregate, or fewer characters in a string literal used to initialize an array of known
size than there are elements in the array, the remainder of the aggregate shall be
initialized implicitly the same as objects that have static storage duration.
10 If an object that has automatic storage duration is not initialized explicitly, its value is
indeterminate. If an object that has static storage duration is not initialized explicitly,
then:
— if it has pointer type, it is initialized to a null pointer;
— if it has arithmetic type, it is initialized to (positive or unsigned) zero;
— if it is an aggregate, every member is initialized (recursively) according to these rules;
— if it is a union, the first named member is initialized (recursively) according to these
rules.
If you understand what you're doing, you're not learning anything.
I know all that, I was just wondering if you were allowed to stick in a trailing comma. I saw someone here do it and was wondering if it was valid.
Thanks.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
It's valid. But it's also ugly IMHO
If you understand what you're doing, you're not learning anything.
Yes, the trailing comma in initializers is valid.
Insert obnoxious but pithy remark here
I figured that out already, from the other posts.
Same here.It's valid. But it's also ugly IMHO
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
I also think it's not a very explicit way of saying what you want to do.
You're better off saying:
Array[0] = 0;
Sent from my iPad®
No, actually you're not better off that way. All that does is set the first element. The other version initializes all elements in the array.
Quzah.
Hope is the first step on the road to disappointment.
Ah, but it doesn't. Not the code I compiled, anyway, if you look at my example in my above post.
The guy who initially used the 0, appeared to want to initialize all the elements in the array, but I don't think he got what he expected.
Last edited by SlyMaelstrom; 11-21-2005 at 06:23 PM.
Sent from my iPad®
I think it's actually used more often to simulate:Originally Posted by SlyMaelstrom
By doing something like:Code:int array[50]; memset(array, 0, sizeof(int) * 50);
Code:int array[50] = { 0 };
If you understand what you're doing, you're not learning anything.
Yes I know he wanted it to work like:
...but it doesn't. That's what I'm saying when I said he didn't get the results he appeared to he wanted.Code:int array[50] = { 0 };
Adding the comma seems to imply to the compiler that you're trying to initialize a specific element of the array similar to doing:
Code:int array[5] = { 0, 1, 2, 3, 4 };
Sent from my iPad®
No it doesn't.As I said, it initializes all elements in my example, one in yours.Code:#include<stdio.h> int main( void ) { int array[ 10 ] = { 1, }; int x; for( x = 0; x < 10; x++ ) printf("array[ %d ] is %d\n", x, array[ x ] ); return 0; } /* array[ 0 ] is 1 array[ 1 ] is 0 array[ 2 ] is 0 array[ 3 ] is 0 array[ 4 ] is 0 array[ 5 ] is 0 array[ 6 ] is 0 array[ 7 ] is 0 array[ 8 ] is 0 array[ 9 ] is 0 */
[edit]
Let's clarify: Are you trying to say, "I think he thinks it will put 2 into every single element.", or what exactly?
The comma has absolutely no effect whatsoever. The standard dictates that if you initialize an element in an array, at declaration time, all those you don't initialize will be set to zero.
So, are you saying he doesn't know that, or what?
[/edit]
Quzah.
Last edited by quzah; 11-21-2005 at 06:37 PM.
Hope is the first step on the road to disappointment.
This looks like 0-initialized to me:
Code:2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.