Newsgroups: comp.lang.c++
Path: utzoo!utgpu!cunews!bnrgate!scrumpy!bnrmtl@bnr.ca!pat
From: pat@bnrmtl.bnr.ca (Patrick Smith)
Subject: Re: bug in istrstream example?
Message-ID: <1991May13.220553.28817@scrumpy@.bnr.ca>
Sender: news@scrumpy@.bnr.ca (USENET (SY))
Reply-To: bnrmtl!pat@larry.mcrcim.mcgill.edu
Organization: Bell Northern Research, Montreal, Canada.
References: <1991May11.015836.1556@neon.Stanford.EDU> <38767@ditka.Chicago.COM>
Date: Mon, 13 May 91 22:05:53 GMT

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.
