Sunday, July 12, 2009

String constant and pointers in C++?

Please look at the code below.





char *ptr;


ptr = "Pointers add power to C++.\n";


cout %26lt;%26lt; ptr;





Why does it print out "Pointers add power to C++"?





My book tells me that that "Pointers add power to C++" yields a pointer to its entry in the string table. So why doesn't the pointer print out the address of the string constant rather than the content itself?





So if the string constant is stored in the address 0x22ff50 shouldn't the program print out 0x22ff50?

String constant and pointers in C++?
If the string constant is stored at 0x22ff50, then what is actually stored in ptr is 0x22ff50. Fortunately for your sanity (and mine) cout is smart enough to know that when you do cout %26lt;%26lt; ptr you want to print the string at the address stored in ptr. Like the above answer says, if you want the address to print, you must cast it as a pointer to void - cout %26lt;%26lt; (void *)ptr;





This works through operator overloading. This answer is already too long, and explaining overloading would make it WAY too long. You will learn about it later, when you learn about classes. Basically, when you do cout %26lt;%26lt; myVar; what happens depends on what type myVar is.





Also, there is one more piece of magic that makes this work. The statement:


ptr = "Pointers add power to C++.\n";


is technically invalid. As you know, pointers store addresses, not strings! A compiler extension allows this statement to work. When you assign a string constant to a pointer to char, the compiler will allocate memory to store the string, put the string in that memory, and store the address of the first character in your pointer. Here is what happens behind the scenes: (not what actually happens, but a way to visualize it)





char *ptr;


//allocate memory to hold the string (28 chars)


//notice we need one extra char - the null character '\0' will be put at the end of the string.


char *aString = new char[28];


//store the string in memory


//You'll learn about pointer math shortly if you haven't already


*aString = 'P';


*(aString + 1) = 'o';


*(aString +2) = 'i';


*(aString + 3) = 'n';.


.


.


*(aString + 22) = 'C';


*(aString + 23) = "+";


*(aString + 24) = '+';


*(aString + 25) = '.';


*(aString + 26) = '\n';


//append the null character


*(aString + 27) = '\0';


ptr = aString;


cout %26lt;%26lt; ptr;


//at the very end of your code:


delete[] aString;





//end of diatribe, hope this helps...
Reply:This may vary by compiler, but at least with gcc, when you have





char *j = "hello";





the compiler returns an address to the static memory location of the string constant, not to a dynamically allocated array. The conversion from the string constant acts more like a "const char *". Report It

Reply:all other pointers except for character pointers shows address .


but character pointer acts as simple character array in c/c++
Reply:Pointers are typed -- meaning it is a char * pointer, not a "generic" pointer like a "void *" pointer. Thus, the compiler is aware you want a character pointer, and prints the string. If you were to cast this to a (void*) pointer it would not do the same thing -- even though you are pointing at the same memory location.


No comments:

Post a Comment