Last night I wrote a short C LanguageProgram(In a Linux environment), compilation is successful, but a segment error is always reported during running. I was very depressed becauseCodeNot long. The main function has the following sentence:
Char * STR = "epmzm bpmzm QA eqtt bpmzm qa I EIG ";
The code in the future will operate on this string (the characters in the string are modified ). But changed
Char STR [] = "epmzm bpmzm QA eqtt bpmzm qa I EIG"; then it will run normally.
I thought for a long time and discussed it with my classmates. Finally, I figured out the reason.
The first method is to first create a String constant and point it to it with the STR pointer, which of course has no syntax error. However, a String constant can only be referenced and cannot be modified. Therefore, an error occurs during running.
The second method first creates a character array STR and initializes it. At this time, "epmzm bpmzm QA eqtt bpmzm qa I EIG" is no longer a String constant.
In this way, I come to the conclusion that the constants defined in the C language cannot be modified. Remember