incomplete initialization of array

M

Mark

Hi all :

I thought that the following code will initialilze every element of array,
but it appears that it does so only for the 0th index.

#define MAX_LEN 10
char name[MAX_LEAN + 1] = { '\0', };

Somehow I was assured that such syntax, i.e. value followed by comma will
work for all elements of arrays. Seems I was wrong and only memset() will do
what I need. Does the standard clearly says that such construction will
never work?

Thanks !

Mark
 
B

Ben Pfaff

Mark said:
I thought that the following code will initialilze every element of array,
but it appears that it does so only for the 0th index.

#define MAX_LEN 10
char name[MAX_LEAN + 1] = { '\0', };

Assuming that MAX_LEAN is a typo for MAX_LEN, this will
initialize all 11 elements to 0.

C99 says:

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

Ben Bacarisse

Mark said:
I thought that the following code will initialilze every element of array,
but it appears that it does so only for the 0th index.

#define MAX_LEN 10
char name[MAX_LEAN + 1] = { '\0', };

Somehow I was assured that such syntax, i.e. value followed by comma will
work for all elements of arrays. Seems I was wrong and only memset() will do
what I need. Does the standard clearly says that such construction will
never work?

As the other Ben has said, it will work. The comma has nothing to do
with it, by the way. It would be interesting to know what has made you
think it is not working. That is where the problem lies, not with the
array declaration.
 
J

J. J. Farrell

Mark said:
I thought that the following code will initialilze every element of array,

You thought correctly.
but it appears that it does so only for the 0th index.

How does that appear? Something else must be happening between the
initialization and the appearance.
#define MAX_LEN 10
char name[MAX_LEAN + 1] = { '\0', };

If this is the actual code rather than a typo, then the array is only
one element in length. Is that what has confused you?
Somehow I was assured that such syntax, i.e. value followed by comma will
work for all elements of arrays.

The comma has nothing to do with it. You are allowed to leave a comma on
the end of a list of initializers, but it doesn't change anything about
the initialization.
Seems I was wrong and only memset() will do what I need.

No, you were more or less right.
Does the standard clearly says that such construction will never work?

No, the Standard clearly says that such a construction must always work.
The comma is redundant and looks a bit silly to me, I'd leave it off.
 
H

Harald van Dijk

Mark said:
#define MAX_LEN 10
char name[MAX_LEAN + 1] = { '\0', };

If this is the actual code rather than a typo, then the array is only
one element in length. Is that what has confused you?

I don't understand what you mean here. Yes, MAX_LEN and MAX_LEAN are
spelt differently, but that should result in a compiler error, not an
array of a different length.
 
B

BartC

Mark said:
Hi all :

I thought that the following code will initialilze every element of array,
but it appears that it does so only for the 0th index.

#define MAX_LEN 10
char name[MAX_LEAN + 1] = { '\0', };

Somehow I was assured that such syntax, i.e. value followed by comma will
work for all elements of arrays. Seems I was wrong and only memset() will
do what I need. Does the standard clearly says that such construction will
never work?

I don't know about the standard. But some experimentation shows only the
first element is set. The rest seem to be zeros.

If you're only interested in zeros anyway, then it doesn't matter.
 
B

Ben Bacarisse

BartC said:
Mark said:
Hi all :

I thought that the following code will initialilze every element of
array, but it appears that it does so only for the 0th index.

#define MAX_LEN 10
char name[MAX_LEAN + 1] = { '\0', };

Somehow I was assured that such syntax, i.e. value followed by comma
will work for all elements of arrays. Seems I was wrong and only
memset() will do what I need. Does the standard clearly says that
such construction will never work?

I don't know about the standard. But some experimentation shows only
the first element is set. The rest seem to be zeros.

But in the OP's case the explicit initialiser is also zero so maybe some
of the answers might have been a little confusing. "It works" means
that the code does what the OP seems to want, but it does not mean that
an array can be initialised with all non-zero values by simply giving
one non-zero initialiser.

The standard says that the remaining elements are initialised as if the
object had static storage duration -- that's a shorthand for zero (the
element type's zero value, not all-bits-zero though they may be the
same). Any non-zero elements have to be explicitly set.
 
J

Joe Pfeiffer

BartC said:
Mark said:
Hi all :

I thought that the following code will initialilze every element of
array, but it appears that it does so only for the 0th index.

#define MAX_LEN 10
char name[MAX_LEAN + 1] = { '\0', };

Somehow I was assured that such syntax, i.e. value followed by comma
will work for all elements of arrays. Seems I was wrong and only
memset() will do what I need. Does the standard clearly says that
such construction will never work?

I don't know about the standard. But some experimentation shows only
the first element is set. The rest seem to be zeros.

Well, yes -- if you specify any at all, the ones you specify are set to
whatever you want. The others are all set to 0.
If you're only interested in zeros anyway, then it doesn't matter.

It matters, since otherwise the contents of the array are effectively
random.
 
B

BartC

Joe Pfeiffer said:
Well, yes -- if you specify any at all, the ones you specify are set to
whatever you want. The others are all set to 0.


It matters, since otherwise the contents of the array are effectively
random.

I meant that, if element 0 *was* initialised to zero, the other elements are
coincidentally set to zero as well.

This won't work for a value other than zero, as it wouldn't be propagated
into the remaining elements as seemed (to me) to be implied if you didn't
read the other replies carefully:

int a[5]={0}; (0,0,0,0,0) as expected
int a[5]={1}; (1,0,0,0,0) not (1,1,1,1,1) as might be assumed.
 
J

James Kuyper

Joe Pfeiffer said:
Well, yes -- if you specify any at all, the ones you specify are set to
whatever you want. The others are all set to 0.
....
int a[5]={1}; (1,0,0,0,0) not (1,1,1,1,1) as might be assumed.

You're right, that assumption is incorrect. But I didn't realize that's
what you were talking about. Is that what Mark was talking about, too?
 
J

J. J. Farrell

Harald said:
Mark said:
#define MAX_LEN 10
char name[MAX_LEAN + 1] = { '\0', };
If this is the actual code rather than a typo, then the array is only
one element in length. Is that what has confused you?

I don't understand what you mean here. Yes, MAX_LEN and MAX_LEAN are
spelt differently, but that should result in a compiler error, not an
array of a different length.

Apologies, I must have been asleep when posting. My mind had wandered
off into the messy areas of preprocessor constant expressions involving
macros which may or may not be defined, and may be defined as empty. I
had to fight with that a lot in my first C project, and it left deep
scars. I don't know which genius added "defined" to C89, but I remember
wanting to buy him a pint; it made that sort of thing much easier.

My comment would have made sense if there were a

#define MAX_LEAN

or equivalent before that code.
 
J

J. J. Farrell

James said:
Joe Pfeiffer said:
I don't know about the standard. But some experimentation shows only
the first element is set. The rest seem to be zeros.
Well, yes -- if you specify any at all, the ones you specify are set to
whatever you want. The others are all set to 0.
...
int a[5]={1}; (1,0,0,0,0) not (1,1,1,1,1) as might be assumed.

You're right, that assumption is incorrect. But I didn't realize that's
what you were talking about. Is that what Mark was talking about, too?

Hard to tell since the example he gave would have had the same result
for both possibilities. For the original example, at least, "not
working" couldn't mean he'd made the wrong assumption described here.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,952
Messages
2,570,115
Members
46,702
Latest member
TheronM691

Latest Threads

Top