Possible Duplicates:
Why does simple C code receive segmentation fault?
Modifying C string constants?
Why does this code generate an access violation?
int main()
{
char* myString = "5";
*myString = 'e'; // Crash
return 0;
}
CARVIEW |
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about CollectivesTeams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about TeamsPossible Duplicates:
Why does simple C code receive segmentation fault?
Modifying C string constants?
Why does this code generate an access violation?
int main()
{
char* myString = "5";
*myString = 'e'; // Crash
return 0;
}
*mystring is apparently pointing at read-only static memory. C compilers may allocate string literals in read-only storage, which may not be written to at run time.
String literals are considered constant.
The correct way of writing your code is:
const char* myString = "5";
*myString = 'e'; // Crash
return 0;
You should always consider adding 'const' in such cases, so it's clear that changing this string may cause unspecified behavior.