Re: Array reinitialize
From: Chris \( Val \) (chrisval_at_bigpond.com.au)
Date: 05/18/04
- Next message: H. Stoelinga: "c++ pointer initialization or assignment"
- Previous message: Russell Hanneken: "Re: Linux"
- In reply to: Karl Heinz Buchegger: "Re: Array reinitialize"
- Next in thread: Karl Heinz Buchegger: "Re: Array reinitialize"
- Reply: Karl Heinz Buchegger: "Re: Array reinitialize"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 19 May 2004 00:35:06 +1000
"Karl Heinz Buchegger" <kbuchegg@gascad.at> wrote in message
news:40AA18E0.B75B771E@gascad.at...
| "Chris ( Val )" wrote:
| >
| > "Jacques Labuschagne" <jacques@clawshrimp.com> wrote in message
| > news:3Xjqc.2814$FN.292956@news02.tsnz.net...
| > | Chris ( Val ) wrote:
| > |
| > | > "Leor Zolman" <leor@bdsoft.com> wrote in message
| > | > news:0dtia0ti3sug7ckuak6miuuu6g0cgtj811@4ax.com...
| > | > | On Tue, 18 May 2004 10:48:37 +1000, Craig Bumpstead <cbumpste@yahoo.com.au>
| > | > | wrote:
[snip]
| > | 8.5.2.2 of the C++ standard says "There shall not be more initializers
| > | than there are array elements." The C99 standard, on the other hand,
| > | says (in 6.7.8.14) that "An array of character type may be initialized
| > | by a character string literal, optionally enclosed in braces. Successive
| > | characters of the character string literal (including the terminating
| > | null character *if there is room* or if the array is of unknown size)
| > | initialize the elements of the array."
| >
| > Hi Jacques.
| >
| > Thanks for the response, but I'm not sure if the
| > above really answers what I was asking.
| >
| > For example - Leor stated the following:
| >
| > char chArray[20] = "aaaaaaaaaaaaaaaaaaaa"; /* C only! */
| > /* In C++, there would be one too many elements */
| >
| > ...but, I don't see anything wrong with the above line
| > in C++. Where is the "one too many elements" ?
|
| The "aaaa...aaa" contains 20 characters 'a' and the array
| is 20 characters long. So all those 'a' will fit in there
| but not the terminating '\0'.
|
| In C this is not a problem: The language allows to not
| have the terminating '\0' even in this case. But not
| so in C++. "aaa..aaa" is treated as C-style string which
| has a terminating '\0' in any case. Thus in C++ the string
| is really 21 characters long.
|
| I haven't checked the C++ standard document what it has
| to say about it, but since usually Leor knows what he is talking
| about I trust him.
Hi Karl.
Of course, Leor knows what he is talking about,
but I often get mixed up ;-).
I didn't realise that a literal in C++ was always
considered to be nul terminated, when initialising
an array via an aggregate initialiser, specifically
when providing an array size to boot.
I thought it would just leave off the '\0'.
Thanks.
Chris Val
- Next message: H. Stoelinga: "c++ pointer initialization or assignment"
- Previous message: Russell Hanneken: "Re: Linux"
- In reply to: Karl Heinz Buchegger: "Re: Array reinitialize"
- Next in thread: Karl Heinz Buchegger: "Re: Array reinitialize"
- Reply: Karl Heinz Buchegger: "Re: Array reinitialize"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|