Quick question about initializing arrays in C

Discussion in 'Mac Programming' started by mac2x, Oct 5, 2010.

  1. macrumors 65816

    Joined:
    Sep 19, 2009
    #1
    Hi all,

    Quick question:

    Is this a correct way to initialize all elements of an array to zero?

    Code:
    double name[ size ] = {0.0}
    My instructor said that this works, but gcc does not like it, and returns an error. And in fact, everything I have read in books and on the 'net states that this shortcut is not available in C.

    This fellow teaches C++ and Java as well, so maybe it's a mix-up?

    Thanks!
     
  2. macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #2
    That would work in 'C++' but not 'C'.
     
  3. thread starter macrumors 65816

    Joined:
    Sep 19, 2009
    #3
    Thanks!
     
  4. macrumors 603

    Joined:
    Aug 9, 2009
    #4
    The correct answer is: It depends on the nature of 'size'.

    If size is a compile-time constant, it works fine in C:
    Code:
    #include <stdio.h>
    
    #define size 10
    
    int main (int argc, const char * argv[]) 
    {
    	double name[ size ] = {0.0};
    	
    	printf( "sizeof name: %d \n", sizeof(name) );
    
        return 0;
    }
    
    Note that size can be any defined value, including a calculation, as long as it's an expression that is a compile-time constant.

    If size is a const int, however, then it doesn't work in C:
    Code:
    #include <stdio.h>
    
    const int size = 23;
    
    int main (int argc, const char * argv[]) 
    {
    	double name[ size ] = {0.0};
    	
    	printf( "sizeof name: %d \n", sizeof(name) );
    
        return 0;
    }
    
    Error message:
    Code:
    init.c:7: error: variable-sized object may not be initialized
    
    I think this shows one of the problems with posting too little code and no actual error messages: context has to be guessed at in order to answer the question.
     
  5. thread starter macrumors 65816

    Joined:
    Sep 19, 2009
    #5
    Thanks for the explanation. I actually didn't have any code to post as I have had very little problem with my latest assignment after clearing up a few things early on. I actually rewrote it extensively to use arrays, since we just covered that. :D

    Just wondering about this; but sorry I didn't provide deeper context.

    But, one learns something every day!
     
  6. thread starter macrumors 65816

    Joined:
    Sep 19, 2009
    #6
    Checked with instructor today. This works in C too (and I tested it):

    Code:
    double name[100] = {0.0};
     
  7. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #7
    That falls under "compile-time constant".

    -Lee
     
  8. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #8
    What is "size"?
    Where is the declaration?
    What error?
    What shortcut are you talking about?

    Did your instructor ever tell you that when you ask questions, you have to say _very precisely_ what you are asking, because slightly different questions could have very different answers, and we can't guess what you mean?


    Slight change, and it works in both C and C++:

    Code:
    enum { size = 23 };
    
     
  9. macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #9
    calloc() should zero out the memory it reserves, so in the future when you need to make variable sized arrays try to remember that.

    You can also use bzero() (*deprecated*) and memset() to zero out some memory.
     

Share This Page