Home > Back-end >  Write access conflicts often pointer
Write access conflicts often pointer

Time:03-07

 # include 
using namespace std;
Int main ()
{
Char * const name="Chen";
cout The name [3]='a';
cout }


According to the textbook written often pointer, why can have write access to abnormal ah

CodePudding user response:

Char STR [20]="Chen";
Char * const name=STR;
Apparently are not in accordance with the book to write,

CodePudding user response:

Are you sure your problem is?
Please run the code,
Char * const name=new char [6] {} "hello".
The name [0]='a';
Cout CodePudding user response:

Const modifier next to it on the right side of the identifier
Char * const STR. Mean STR is read-only, but STR to address is not necessarily a read-only
That is to say,
Char * const STR=new char [10].
Then
STR=new char [1].//here cannot compile, because STR cannot point to other address
But
STR [0]='a'; Can be
The second
Char * const STR="hello";//this is pass it on more strict compiler to compile, because we can not "hello" is a const char *, cannot initialize const char *; The two types of different