![]() |
incomplete initialization of array
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 |
Re: incomplete initialization of array
"Mark" <mark_cruzNOTFORSPAM@hotmail.com> 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_ |
Re: incomplete initialization of array
"Mark" <mark_cruzNOTFORSPAM@hotmail.com> 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. |
Re: incomplete initialization of array
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. |
Re: incomplete initialization of array
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. |
Re: incomplete initialization of array
"Mark" <mark_cruzNOTFORSPAM@hotmail.com> wrote in message news:j64u3k$op3$1@speranza.aioe.org... > 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 |
Re: incomplete initialization of array
"BartC" <bc@freeuk.com> writes:
> "Mark" <mark_cruzNOTFORSPAM@hotmail.com> wrote in message > news:j64u3k$op3$1@speranza.aioe.org... >> 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. |
Re: incomplete initialization of array
"BartC" <bc@freeuk.com> writes:
> "Mark" <mark_cruzNOTFORSPAM@hotmail.com> wrote in message > news:j64u3k$op3$1@speranza.aioe.org... >> 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. |
Re: incomplete initialization of array
"Joe Pfeiffer" <pfeiffer@cs.nmsu.edu> wrote in message
news:1b62k89x3v.fsf@pfeifferfamily.net... > "BartC" <bc@freeuk.com> 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 |
Re: incomplete initialization of array
On 10/01/2011 07:55 PM, BartC wrote:
> "Joe Pfeiffer" <pfeiffer@cs.nmsu.edu> wrote in message > news:1b62k89x3v.fsf@pfeifferfamily.net... >> "BartC" <bc@freeuk.com> 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 |
| All times are GMT. The time now is 08:29 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.