#include <iostream>
usingnamespace std;
int main()
{
// This works fine:
char myStr[] = "text";
myStr[0] = 'n';
cout << myStr << endl;
// This does not work:
char* myStr2 = "text";
myStr2[0] = 'n'; // This line results in a runtime error!!!
cout << myStr2 << endl;
return 0;
}
I can't understand why the second part doesn't work. There is no complaint from the compiler (Visual Studio 2008 Express Edition). For what I have understood there is no difference between the two declarations in the above example. I guess I have missed some fundamental part of how char arrays work. Can someone explain to me what the difference is between the two declarations?
When you declare char myStr[] you are declaring an array of chars (which is accessible to be both read and written), and this array is initialized to some sequence of characters (ie, the value of the literal "text" is copied to the elements in this array).
While when you declare char * myStr2, you are declaring a pointer that points directly to some constant literal - not a copy, and these can only be read.
The compiler does not complain because in both cases myStr is a pointer to a char, so myStr[0] is valid in both cases. What is different is the thing they point to.
Okey, so there is some rule in C++ that states that a char* initialized by a string literal is a constant and can't later be changed but an array of chars initialized by a string literal is not regarded as a constant and hence allowed to be changed.
But there got to be more to it because in some ways char-pointers (char*) seem to be treated different than for example int-pointers (int*).
Particularly printing the value of the variable yields different output depending on if it's a pointer-to-int or if it's a pointer-to-char. pInt contains the address of the int it's pointing to which is how pointer variables are supposed to work. pChar and pStr do not contain addresses (if you print the values using cout). Instead they contain character literals and cout print them as a string.
Is there some logic in this behavior? I mean, why should the value of a pointer-to-char variable be treated in any other way than the value of a pointer-to-int or a pointer-to-float?
its because the << operator is overloaded for the type char* in order to print all the string. It will print all characters from the one pointed by pChar until it finds a '\0' character, i.e. a character that means end of string.
So if you do this :
char hello[8] = "HelloW";
you are storing all characters from "HelloW" in an array of char plus a '\0' char which is added by the compilator (be cautious to have enough memory allocated for it !)
So if I get you right, the reason that the value of char* variable is printed as a string is that << is constructed to do just that for a char*. In other words it has nothing to do with different interpretations of char* and int* variables.
Is it correct to say that all kinds of C++ pointer variables contains an address? If you want to look at the value of pointer variable (i.e. an address) you just have to choose a method that shows you the value. And for char* that is not the << operator. Correct?
In that case: Which is the correct method for char* to do that?
Yes Martin, that did the trick. Thanks again for your input. After fiddling around a bit with char-pointers and char-arrays I think I'm starting to getting the hang of it. It's definitely not trivial and I guess it takes a lot of practise to really understand it.
Anyway, thanks again all for your input. Maybe I some day will be a real C++ programmer myself :-)