Board index » cppbuilder » Re: Why does this work?

Re: Why does this work?


2005-11-01 01:49:19 AM
cppbuilder7
On Mon, 31 Oct 2005 17:49:06 +0100, Knut Olsen-Solberg wrote:
Quote
Of course I have Stroustrups "The C++ Programming Language (third
edition)", but my book does not have a chapter 8.3.4, and chapter
4.2 is a short notice about booleans. So I still wander what is the
title of your book?
Alan referred to the C++ Language specification -- the standards
document.
webstore.ansi.org/ansidocstore/product.asp
snipurl.com/jbuz
--
liz
 
 

Re:Re: Why does this work?

"liz" < XXXX@XXXXX.COM >wrote in message
Quote
webstore.ansi.org/ansidocstore/product.asp
snipurl.com/jbuz
Do you know where there's a link to the full text for those
of us who are too cheap to shell out the bucks?
 

Re:Re: Why does this work?

"Duane Hebert" < XXXX@XXXXX.COM >wrote:
Quote
"liz" < XXXX@XXXXX.COM >wrote in message

>webstore.ansi.org/ansidocstore/product.asp
>snipurl.com/jbuz

Do you know where there's a link to the full text for those
of us who are too cheap to shell out the bucks?
1) It's a copyrighted work - so as such, any such link would be illegal.
2) $18 isn't so much:
webstore.ansi.org/ansidocstore/product.asp
(Why there's also an option to buy a $288 pdf, I don't know. Maybe the
bits are all gold-plated.)
3) You're could also try buying the Wiley edition - a nice solid
hardback at a sensible computer-book price.
Alan Bellingham
--
ACCU Conference 2006 - 19-22 April, Randolph Hotel, Oxford, UK
 

{smallsort}

Re:Re: Why does this work?

"Alan Bellingham" < XXXX@XXXXX.COM >wrote in message
Quote
"Duane Hebert" < XXXX@XXXXX.COM >wrote:

>"liz" < XXXX@XXXXX.COM >wrote in message
>
>>webstore.ansi.org/ansidocstore/product.asp
>>snipurl.com/jbuz
>
>Do you know where there's a link to the full text for those
>of us who are too cheap to shell out the bucks?

1) It's a copyrighted work - so as such, any such link would be illegal.
2) $18 isn't so much:

webstore.ansi.org/ansidocstore/product.asp

(Why there's also an option to buy a $288 pdf, I don't know. Maybe the
bits are all gold-plated.)
It's the $288 that I was concerned about.
 

Re:Re: Why does this work?

"Duane Hebert" < XXXX@XXXXX.COM >wrote:
Quote
It's the $288 that I was concerned about.
I'm not surprised. There was a hard-copy version that was even more
expensive, which is why ACCU persuaded Wiley to produce a
sensibly-priced hardback
Alan Bellingham
--
Team Thai Kingdom
<url:www.borland.com/newsgroups/>Borland newsgroup descriptions
<url:www.borland.com/newsgroups/netiquette.html>netiquette
 

Re:Re: Why does this work?

Alan Bellingham wrote:
Quote
:
:
function wanting pointer-to-type, and that it would do the conversion as
required. It is this that is referred to in 4.2 - that is, section 4.2
of the C++ Language Standard, document ISO/IEC 14882:1998(E), available
from your country's standards organisation.

(Or get the excellent hardcopy version from Wiley, which we at ACCU
persuaded both Wiley and the BSI would be a very good idea:
<eu.wiley.com/WileyCDA/WileyTitle/productCd-0470846747.html>.
Anyone attempting to teach C++ without the standard available is in the
same position as a biblical teacher who doesn't have a Bible.)
I have Stroustrups "The C++ Programming Language" (third edition) where Bjarne says in the preface: "This book introduces the standard C++ and the key programming and design techniques supported by C++". I have used it like a sort of "bible", but there is evidently more than one bible on the market.
I have now had a glance into 4.2 and 8.3.4, and I could not see anything there forcing me to think of Str as a variable. It is called "the identifier".
PS.
(I use a dictionary and may not always say in English what I think in Norwegian...)
Thanks Knut
 

Re:Re: Why does this work?

Alan Bellingham wrote:
Quote
"Duane Hebert" < XXXX@XXXXX.COM >wrote:


>"liz" < XXXX@XXXXX.COM >wrote in message
>
>
>>webstore.ansi.org/ansidocstore/product.asp
>>snipurl.com/jbuz
>
>Do you know where there's a link to the full text for those
>of us who are too cheap to shell out the bucks?
See what I found:
www.kuzbass.ru/docs/isocpp/
Knut
 

Re:Re: Why does this work?

Knut Olsen-Solberg < XXXX@XXXXX.COM >writes:
Quote
liz wrote:

>On Fri, 28 Oct 2005 18:55:54 +0200, Knut Olsen-Solberg wrote:
>
>>My students however write very often like this: cout << &Str <<
>>'\n'; -and my problem is that this works! Can anyone see any
>>logical reason for why this works?
Sorry to jump in late, but I was in DC last week...
What do you expect to see happening here?
Quote
Sorry Liz. Str is not a variable in RAM. It is an address, and &Str
is the address of an address... which is nonsense.
Str _is_ a variable, and its type is array of char. All variables
have an address, and arrays have a unique property of implicit
conversion to "pointer to T", where T is the element type. Don't
confuse the pre-converted type with the post-converted type. An array
and a pointer are strictly different things in C++!
A pointer holds nothing but an address. An array holds the actual
data.
For example, slightly modifying your program:
#include <iostream>
using namespace std;
int main()
{
char Str[]="Hello";
cout << Str << '\n'; //1
cout << &Str << '\n'; //2
return 0;
}
line //1 implicitly converts str to a pointer to the first element,
which has the the type char*, and thus is treated like a
null-terminated string. Since all string literals have a Null byte
after them (in this case after the 'o' in Hello), it prints out
correctly.
line //2 takes the address of Str (which is a variable that lives in
memory somewhere), and the result is of type
"pointer-to-array-of-char", which has no directly overloaded operator<<()
function for inserting into the ostream. Overload resolution now
converts the pointer into a void*, and invokes the operator<<() member
function of the ostream object, and prints out the address as an address.
--
Chris (TeamB);
 

Re:Re: Why does this work?

Knut Olsen-Solberg < XXXX@XXXXX.COM >writes:
Quote
I have now had a glance into 4.2 and 8.3.4, and I could not see
anything there forcing me to think of Str as a variable. It is
called "the identifier".
Please.
You have now been told by a number of knowledgeable people and myself
that your perception is wrong. IMHO, the burden of proof is now with
you. Where does the Standard say that
char Str[10];
declares the identifier Str to name a pointer?
 

Re:Re: Why does this work?

Thomas Maeder [TeamB] wrote:
Quote
Knut Olsen-Solberg < XXXX@XXXXX.COM >writes:


>I have now had a glance into 4.2 and 8.3.4, and I could not see
>anything there forcing me to think of Str as a variable. It is
>called "the identifier".


Please.

You have now been told by a number of knowledgeable people and myself
that your perception is wrong. IMHO, the burden of proof is now with
you. Where does the Standard say that

char Str[10];

declares the identifier Str to name a pointer?
I never said that. I said Str was an address (a constant number of type char*, like 17 is a constant number of type int). A pointer is a variable, and that's just what I said it is NOT.
Of course I may be wrong, but can you tell where the Standard says it has to be thought of as a variable?
Knut
 

Re:Re: Why does this work?

Knut Olsen-Solberg < XXXX@XXXXX.COM >writes:
Quote
I never said that. I said Str was an address (a constant number of
type char*, like 17 is a constant number of type int). A pointer is
a variable, and that's just what I said it is NOT.
It's time to clarify terms.
A variable is either
- a named object, or
- a named "non-object" (e.g. if its of reference type)
It can be of
- a fundamental type (int, bool, char, double etc.), or
- a compound type (array, struct, union, pointer, reference, enum etc.)
An address can be taken. It can't be declared.
Declarations such as
int i;
double pi(3.1416);
char Str[10];
declare objects with names, i.e. variables.
Quote
Of course I may be wrong, but can you tell where the Standard says
it has to be thought of as a variable?
?/4
 

Re:Re: Why does this work?

Quote
Str _is_ a variable, and its type is array of char.
I think this is essentially his mistake, he kept saying Str is an alias
(what's his definitition of an alias, I haven't got a clue though)... --
Let's get to the fundamentals. Perhaps this would make it clearer:
address x: Str (a 32-bit integer, which is a pointer to the location of
which the string resides in the memory)
Given the value of Str = y, then,
address y: char [0]
address y+1: char [1]
...
address y+n: char [n]
If you dereference Str (i.e. *Str), you'll get char[0], which is the
contents of Address y.
If you get Str's reference (i.e. &Str), you'll get the address of Str, which
is Address x. If Str is not a variable, specifying &Str will be a random
number??? :)
So the way they have overloaded the << operator definitely makes sense.
Knut, see if you can answer this then (you should be able to if you
understand the above...)
how would you display the address of the first element of the array of char?
cout << __________________ ;
And another question:
If you say there's no such thing as "an address of an address"... then
answer this:
what is a char * * ? or a char * * * ??
Perhaps the misconception you have is this: Instead of calling an address of
an address, start saying it this way: "An address of the location of which
another address is stored". If you're not familiar with pointers, draw out a
memory map.
The fundamental concept of referencing and deferencing in C / C++ is
essentially the core of the language. IMHO, this is what makes the language
so flexible. Sadly, hardly any new hires truely understands this any more...
thanks to those Java {*word*99}. In fact, the question asked by Knut was one of
the interview questions we used when we were looking to hire some new staff.
None got it entirely right.
Back in the real mode world, you even have far and near pointers --
something that I've played with back when I was a {*word*249}... probably one of
the problems with programmers nowadays is that they don't write device
drivers anymore, they have no chance to talk to the hardware directly at
all. I still remember those days we have to write directly to the graphics
card memory to draw a line... and even dealing with 640x480x8-bit, we have
already crossed the 64k boundary and had to start meddling with far
pointers... life is definitely easier now.
 

Re:Re: Why does this work?

Knut,
Let's try another one shall we?
cout << &"hello" << endl;
What does this give us?
And to help you understand further, put a breakpoint at the line after that,
and take the output value from the cout, and bring up the de{*word*81} window.
Go to that address and look for yourself what you've got there.
Hmm... actually, come to think of it.... what you were saying, "an address
of an address", would mean you are specifying the following code:
cout << &&"hello" << endl;
does it work? well, I guess you're right, there's no such thing as an
address of an address... and apparently, the compiler is right as well :)
 

Re:Re: Why does this work?

"Zach Saw" < XXXX@XXXXX.COM >writes:
Quote
Let's try another one shall we?

cout << &"hello" << endl;

What does this give us?
The type of &"hello" is "pointer to array of 6 char const". The
operator<< for void const * should thus be used, and the address of
the array (which, as void *, should be equal the address of its first
element) should be printed.
Quote
Hmm... actually, come to think of it.... what you were saying, "an address
of an address", would mean you are specifying the following code:

cout << &&"hello" << endl;

does it work?
No. &"hello" is an rvalue; you can't take its address.
 

Re:Re: Why does this work?

Thomas Maeder [TeamB] wrote:
Quote
Knut Olsen-Solberg < XXXX@XXXXX.COM >writes:


>I never said that. I said Str was an address (a constant number of
>type char*, like 17 is a constant number of type int). A pointer is
>a variable, and that's just what I said it is NOT.


It's time to clarify terms.

A variable is either
- a named object, or
- a named "non-object" (e.g. if its of reference type)

It can be of
- a fundamental type (int, bool, char, double etc.), or
- a compound type (array, struct, union, pointer, reference, enum etc.)


An address can be taken. It can't be declared.


Declarations such as

int i;
double pi(3.1416);
char Str[10];

declare objects with names, i.e. variables.
Right. And the declaration double *ipt; is another variable, a pointer.
The last line (char Str[10]) defines ten variables, all of type char.
Who says we have to regard Str as the name of a compound type variable (array)?
Here is probably the kernel of the discussion. Can't we live without thinking of the array as a (one) variable?
Knut