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?
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.
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?
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.
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".
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'
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.
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