Reputation: 152
This is my code:
char * name[]={"Dileep","Avina"};
name[0][1]='A';
here name[0] is an char* pointing to string literal "Dileep". So what will be the name[0][1]? Why it is giving me runtime error?
Upvotes: 0
Views: 56
Reputation: 172914
name[0] is an char* pointing to string literal "Dileep".
No, char * name[]
is spurious. "Dileep"
is string literal with type const char[7]
, and your code is not allowed since C++11 because of the conversion from string literal to char *
. char * name[]={"Dileep","Avina"};
should be const char * name[]={"Dileep","Avina"};
.
So what will be the name[0][1]?
It should be a const char
.
Why it is giving me runtime error?
Modifying string literal is UB.
Upvotes: 0
Reputation: 177610
"Dileep" and "Avina" are string constants. Trying to change them is undefined behavior. If the OS puts them in read-only memory, you will get a fault.
Upvotes: 3