PDA

View Full Version : Quick question about initializing arrays in C




mac2x
Oct 5, 2010, 11:30 PM
Hi all,

Quick question:

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

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!



lloyddean
Oct 6, 2010, 12:10 AM
That would work in 'C++' but not 'C'.

mac2x
Oct 6, 2010, 12:24 AM
Thanks!

chown33
Oct 6, 2010, 12:50 AM
The correct answer is: It depends on the nature of 'size'.

If size is a compile-time constant, it works fine in C:
#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:
#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:
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.

mac2x
Oct 6, 2010, 01:53 AM
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!

mac2x
Oct 6, 2010, 08:35 PM
Checked with instructor today. This works in C too (and I tested it):

double name[100] = {0.0};

lee1210
Oct 6, 2010, 08:41 PM
That falls under "compile-time constant".

-Lee

gnasher729
Oct 7, 2010, 02:04 AM
Hi all,

Quick question:

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

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!

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?



If size is a const int, however, then it doesn't work in C:
const int size = 23;



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

enum { size = 23 };

jared_kipe
Oct 7, 2010, 11:22 AM
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.