[try Beta version]
Not logged in

 
pointer documentation example not working

Jun 13, 2011 at 8:10pm
Im currently reading this:
http://www.cplusplus.com/doc/tutorial/pointers/

there i can see this:

As in the case of arrays, the compiler allows the special case that we want to initialize the content at which the pointer points with constants at the same moment the pointer is declared:


 
char * terry = "hello"; 




Thing is it doesnt work... I assume it has to do with the compiler since it clearly states that "the compiler allows the special case..." so the question is how do i determine if my compiler allows that special declaration?

I am using visual express 2010.
Jun 13, 2011 at 8:17pm
Then you have a good compiler...just put a "const" in front of the char and it'll work.
Jun 13, 2011 at 8:26pm
Dear raptoX u don't want to bother your self with the compiler at this moment right ..
What do u want from the program do u want:
To make terry of type char point to the array of characters as u can see the fifer in the tutorial.
Or u can make Terry astring pointer and then assigne to it the hello word.
You gust have to make som substitutions to make your program acomplet story.
Jun 13, 2011 at 8:43pm
with firedraco suggestion it's compiling. But terry still = "hello" and not the pointer... am i expecting the correct results or is terry = "hello" the correct result?
Jun 13, 2011 at 8:49pm
How did you get that not to work? All versions of VC++ should accept char * terry = "hello"; as valid C++ (because it is).
I just tested it and 2010 does accept it.
Jun 13, 2011 at 9:00pm
Using Visual Studio Express C++ 2010, output says "Hello", and not the reference.
http://i.imgur.com/ZInUa.png
Jun 13, 2011 at 9:04pm
That's the correct behavior. What do you want the output to be?
Jun 13, 2011 at 9:09pm
Well, according to the tutorial:
It is important to indicate that terry contains the value 1702, and not 'h' nor "hello", although 1702 indeed is the address of both of these.

I too am expecting the reference, eg 1702.
Jun 13, 2011 at 9:20pm
Well, although it's not really important at this point in the tutorial, if you want to see the actual memory address, do this:
std::cout <<(void*)terry;

Optional reading: << is in this case a function called operator<<() that is overloaded for when the left operand is an output stream. This function's default behavior for all pointer types as the right operand is to send the memory address in hexadecimal to the output stream. The function has a special exception for const char * and char *. If you pass that as the right operand, it'll send the string that the pointer points to, instead of sending the memory address. This allows doing things like std::cout <<"Hello, World!\n".
Jun 13, 2011 at 9:27pm
closed account (zb0S216C)
char *terry = "Hello" does work in VC++ 2010. Here's two examples of printing terry:

1
2
std::cout << *terry << std::endl; // This will print: H.
std::cout << terry << std::endl;  // This will print: Hello. 

Notice: This post has been reformatted.

Wazzak
Last edited on Jun 13, 2011 at 11:01pm
Jun 13, 2011 at 9:47pm
thanks guys for the help.

and about the question Adabo has... i understand that char *terry wouldnt contain a number but as pointed out by Adabo the tutorial clearly states that:


It is important to indicate that terry contains the value 1702, and not 'h' nor "hello", although 1702 indeed is the address of both of these.


which as framework dully pointed out, is not the case, since *terry would print 'h' and terry would print 'hello'
Jun 13, 2011 at 10:03pm
The tutorial is not wrong here, if that's what you're implying.
Jun 13, 2011 at 10:26pm
I am implying that I am confused. 2 opposing answers means only one can be right. The tutorial states that the identifier "terry" should contain a reference, not the string.

helios, what I read you saying is that your view and the tutorial are conflicting.
Last edited on Jun 13, 2011 at 10:27pm
Jun 13, 2011 at 10:30pm
Correct. terry doesn't contain a string, it contains a memory address that references a string. When you do std::cout <<terry, the standard library sees that you're passing a pointer to a string, and interprets this as "print the string to which the pointer I'm passing points, not the pointer I'm passing".
See if this helps you understand what's going on: http://www.cplusplus.com/forum/general/44090/#msg238682
Jun 13, 2011 at 10:34pm
Thanks. I'll do my best. My noob powers are at max today ;)
Topic archived. No new replies allowed.