philip@pescadero.stanford.edu (Philip Machanick) (05/11/91)
In both Lippman (p. 414) and the cfront 2.0 Library Manual (p 3.12) examples roughly of the form char s[] = "400" istrstream iss (s,sizeof(s)); appear. Presumably, sizeof is a bug (since this will always return the size of a pointer when applied to an array), and what is really required is strlen(s) - or should it be strlen(s)+1 to allow for the null char at the end? Has this been fixed in later editions / printings? Philip Machanick
steve@taumet.com (Stephen Clamage) (05/12/91)
philip@pescadero.stanford.edu (Philip Machanick) writes: >In both Lippman (p. 414) and the cfront 2.0 Library Manual (p 3.12) examples >roughly of the form > char s[] = "400" > istrstream iss (s,sizeof(s)); >appear. Presumably, sizeof is a bug (since this will always return the size of >a pointer when applied to an array), and what is really required is strlen(s) - >or should it be strlen(s)+1 to allow for the null char at the end? >Has this been fixed in later editions / printings? There is nothing to fix. An array is not converted to a pointer when it is the object of the sizeof operator, so the result of sizeof(s) is the number of elements of s times the element size. Given char *s = "hello"; s is declared as a pointer, not an array, so sizeof(s) is the size of the pointer. Given extern char s[]; sizeof(s) is illegal, since the size of s cannot be determined. -- Steve Clamage, TauMetric Corp, steve@taumet.com
comeau@ditka.Chicago.COM (Greg Comeau) (05/14/91)
In article <1991May11.015836.1556@neon.Stanford.EDU> philip@pescadero.stanford.edu (Philip Machanick) writes: > char s[] = "400" > istrstream iss (s,sizeof(s)); >appear. Presumably, sizeof is a bug (since this will always return the size of >a pointer when applied to an array), and what is really required is strlen(s) - >or should it be strlen(s)+1 to allow for the null char at the end? No. The array -> ptr conversion DO NOT occur when an array names is used as the operand of sizeof. The sizeof will return the total number of bytes in s. If s was : char s[] = "12345";, sizeof would return 6. - Greg -- Comeau Computing, 91-34 120th Street, Richmond Hill, NY, 11418 Producers of Comeau C++ 2.1 Here:attmail.com!csanta!comeau / BIX:comeau / CIS:72331,3421 Voice:718-945-0009 / Fax:718-441-2310
pete@borland.com (Pete Becker) (05/14/91)
In article <1991May11.015836.1556@neon.Stanford.EDU> philip@pescadero.stanford.edu (Philip Machanick) writes: >In both Lippman (p. 414) and the cfront 2.0 Library Manual (p 3.12) examples >roughly of the form > char s[] = "400" > istrstream iss (s,sizeof(s)); >appear. Presumably, sizeof is a bug (since this will always return the size of >a pointer when applied to an array), and what is really required is strlen(s) - >or should it be strlen(s)+1 to allow for the null char at the end? > >Has this been fixed in later editions / printings? > >Philip Machanick > The code as written is correct. This is one of the few situations where the name of an array is not converted to a pointer, in both ANSI C and under the ARM. sizoef applied to an array gives the number of bytes in the array! -- Pete
pat@bnrmtl.bnr.ca (Patrick Smith) (05/14/91)
In article <38767@ditka.Chicago.COM>, comeau@ditka.Chicago.COM (Greg Comeau) writes: |> In article <1991May11.015836.1556@neon.Stanford.EDU> philip@pescadero.stanford.edu (Philip Machanick) writes: |> > char s[] = "400" |> > istrstream iss (s,sizeof(s)); |> >appear. Presumably, sizeof is a bug (since this will always return the size of |> >a pointer when applied to an array), and what is really required is strlen(s) - |> >or should it be strlen(s)+1 to allow for the null char at the end? |> |> No. The array -> ptr conversion DO NOT occur when an array names |> is used as the operand of sizeof. The sizeof will return the total number |> of bytes in s. |> |> If s was : char s[] = "12345";, sizeof would return 6. |> |> - Greg I believe you're right (you would certainly know more about the standards than I do), but there are cases where the language makes this a confusing point. I remember once being confused by an example like this: void f( char a[10] ) { cout << sizeof(a) << '\n'; } where the value that got printed was sizeof(char*), not 10. It seems to me that what the compiler (Oregon C++ on a Sun-3/60) did is correct, but it's certainly confusing! The point is that the declaration of a _looks_ like a declaration of an array, but is actually a declaration of a pointer. -- Patrick Smith Bell-Northern Research, Montreal, Canada (514) 765-7914 bnrmtl!pat@larry.mcrcim.mcgill.edu patrick@bnr.ca ... Any resemblance between the above views and those of my employer, my terminal, or the view out my window are purely coincidental.