Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mac2x

macrumors 65816
Original poster
Sep 19, 2009
1,146
0
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!
 

chown33

Moderator
Staff member
Aug 9, 2009
10,740
8,416
A sea of green
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.
 

mac2x

macrumors 65816
Original poster
Sep 19, 2009
1,146
0
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

macrumors 65816
Original poster
Sep 19, 2009
1,146
0
Checked with instructor today. This works in C too (and I tested it):

Code:
double name[100] = {0.0};
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
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!

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:
Code:
const int size = 23;

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

Code:
enum { size = 23 };
 

jared_kipe

macrumors 68030
Dec 8, 2003
2,967
1
Seattle
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.