Thread: Is {0,} okay?

  1. #1
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057

    Is {0,} okay?

    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.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    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.*

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    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.

    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;
    }
    Output:
    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®

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Of course. This is in section 6.7.8 of the ISO C 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.
    And of course we all know that (found in the same section of the standard):
    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.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    It's valid. But it's also ugly IMHO
    If you understand what you're doing, you're not learning anything.

  7. #7
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    Yes, the trailing comma in initializers is valid.
    Insert obnoxious but pithy remark here

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I figured that out already, from the other posts.

    It's valid. But it's also ugly IMHO
    Same here.
    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.

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    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®

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.

  11. #11
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    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®

  12. #12
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by SlyMaelstrom
    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;
    I think it's actually used more often to simulate:
    Code:
    int array[50];
    
    memset(array, 0, sizeof(int) * 50);
    By doing something like:
    Code:
    int array[50] = { 0 };
    If you understand what you're doing, you're not learning anything.

  13. #13
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Yes I know he wanted it to work like:

    Code:
    int array[50] = { 0 };
    ...but it doesn't. That's what I'm saying when I said he didn't get the results he appeared to he wanted.

    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®

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No it doesn't.
    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
    */
    As I said, it initializes all elements in my example, one in yours.

    [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.

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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.

Popular pages Recent additions subscribe to a feed