In this declaration
char* pString = "abcdefg";
the string literal is the record "abcdefg"
not the declared pointer.
The string literal in C has the type char[8]
. Though in the type there is absent the qualifier const
nevertheless you may not change a string literal. Any attempt to change a string literal results in undefined behavior.
In C++ opposite to C string literals have types of constant character arrays.
As for the declared pointer then it points to the first element of the string literal.
In this code snippet
char tmpStr[80];
strcpy(tmpStr, pString);
there is declared character array tmpStr
. You may change the array.
In this statement
strcpy(tmpStr, pString);
elements of the string literal are copied in the character array.
And in the declaration below the declared pointer point to the first element of the array.
char* pString2 = tmpStr;
pString2[0] = 'A';
So you may change the array. You could use even the pointer pString
instead of pString2 like
pString = tmpStr;
pString[0] = 'A';
Pay attention to that you may initialize a character array with a string literal. So you could write for example
char tmpStr[] = "abcdefg";
*tmpStr = 'A';
or
tmpStr[0] = 'A';