Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > incomplete initialization of array

Reply
Thread Tools

incomplete initialization of array

 
 
Mark
Guest
Posts: n/a
 
      09-30-2011
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


 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      09-30-2011
"Mark" <> writes:

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

--
"Programmers have the right to be ignorant of many details of your code
and still make reasonable changes."
--Kernighan and Plauger, _Software Tools_
 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      09-30-2011
"Mark" <> writes:

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

--
Ben.
 
Reply With Quote
 
J. J. Farrell
Guest
Posts: n/a
 
      10-01-2011
Mark wrote:
>
> 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.
 
Reply With Quote
 
Harald van Dijk
Guest
Posts: n/a
 
      10-01-2011
On Oct 1, 4:53*am, "J. J. Farrell" <j...@bcs.org.uk> wrote:
> Mark wrote:
> > #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.
 
Reply With Quote
 
BartC
Guest
Posts: n/a
 
      10-01-2011


"Mark" <> wrote in message
news:j64u3k$op3$...
> 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.

--
Bartc

 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      10-01-2011
"BartC" <> writes:

> "Mark" <> wrote in message
> news:j64u3k$op3$...
>> 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.

--
Ben.
 
Reply With Quote
 
Joe Pfeiffer
Guest
Posts: n/a
 
      10-01-2011
"BartC" <> writes:

> "Mark" <> wrote in message
> news:j64u3k$op3$...
>> 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.
 
Reply With Quote
 
BartC
Guest
Posts: n/a
 
      10-01-2011
"Joe Pfeiffer" <> wrote in message
news:...
> "BartC" <> writes:


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


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.

--
Bartc



 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      10-02-2011
On 10/01/2011 07:55 PM, BartC wrote:
> "Joe Pfeiffer" <> wrote in message
> news:...
>> "BartC" <> writes:

>
>>> 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?
--
James Kuyper
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
initialization of array as a member using the initialization list aaragon C++ 2 11-02-2008 04:57 PM
array initialization in initialization list. toton C++ 5 09-28-2006 05:13 PM
Initialization of non-integral type in initialization list anongroupaccount@googlemail.com C++ 6 12-11-2005 09:51 PM
Initialization via ctor vs. initialization via assignment Matthias Kaeppler C++ 2 07-18-2005 04:25 PM
Default Initialization Vs. Value Initialization JKop C++ 10 09-22-2004 07:26 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57