Re: Array reinitialize

From: Chris \( Val \) (chrisval_at_bigpond.com.au)
Date: 05/18/04


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



Relevant Pages

  • Re: Help a beginner - simple lowercase to uppercase and so on function
    ... And then one to loop across the string calling that function ... copying at to a new array. ... are any characters other than lowercase letters */ ...
    (comp.lang.c)
  • Re: fgets question
    ... documentation didn't say if fgets put \0 after a string literal. ... fgets() has nothing at all to do with string literals. ... within the bounds of the array, then it is not a string. ... strlenon an array of characters that is not a string, ...
    (comp.lang.c)
  • Re: Help creating a random string in Perl
    ... can pick one of the 7 characters in my source string) by using "print ... third and fourth characters to and check that the random number hasn't ... shuffling the array ... The idea here is to swap the current cell with some cell ...
    (perl.beginners)
  • Re: newbie: mapping CHARACTER*2 to INTEGER*2
    ... that destroys the Chinese characters in my input. ... effect the conversion required. ... If MOLD is an array and SIZE is omitted, ... represent the values 4.0 and 1082130432 as the string of binary digits ...
    (comp.lang.fortran)
  • Re: Parsing binary values in the registry
    ... how to take a string variable that could concievably be up to 300 ... and split that into either an array or multiple variables for ... every 32 characters within the original varable. ... I am trying to build a file path from a binary value in the ...
    (microsoft.public.scripting.vbscript)