J
Jason Curl
Hello C coders, et. al,
Please have a look at the snippet of code below.
01 int main(void)
02 {
03 char mystr[] = "Hello world\n";
04 char *pmystr2 = "My String\n";
05
06 mystr[0] = 0;
07 pmystr2[0] = 0;
08
09 return 0;
10 }
Is the second assignment with 'pmystr2' allowed at line 7? There should
be no problem with "Hello world\n" as it is assigned to a char array on
line 3, but the second instance might be writing to a section of memory
that is marked read only, in which the problem originally occurs at line 4.
Or, would the compiler be required to take the constant string "My
String" on line 4, copy it to somewhere it is writable, and then assign
pmystr2 to the address in the new section?
Any insights would help.
Jason.
Please have a look at the snippet of code below.
01 int main(void)
02 {
03 char mystr[] = "Hello world\n";
04 char *pmystr2 = "My String\n";
05
06 mystr[0] = 0;
07 pmystr2[0] = 0;
08
09 return 0;
10 }
Is the second assignment with 'pmystr2' allowed at line 7? There should
be no problem with "Hello world\n" as it is assigned to a char array on
line 3, but the second instance might be writing to a section of memory
that is marked read only, in which the problem originally occurs at line 4.
Or, would the compiler be required to take the constant string "My
String" on line 4, copy it to somewhere it is writable, and then assign
pmystr2 to the address in the new section?
Any insights would help.
Jason.