int main () {
char * s = "Hello,world";
Char *t = "Happy";
strcpy (s,t); Error
return 0;
}
I thought there was nothing wrong, but actually running, there was a segment error,
Later modified char *s = (Cahr *) malloc (12); Ok
Because "Hello,world" is stored in the character constant area, s points to this area, and this area is a const attribute of non-modifiable;
Therefore, there will be a paragraph error when copying again;
A program is divided into:
1. Stack (stack)--the compiler automatically assigns the release, the main storage function parameter value, local variable value, etc. 2. Heap Area (heap)--distributed by programmer, 3. Global or static zone----holds global variables and static variables, which is released by the system at the end of the program. It is divided into the global initialization zone and the global uninitialized zone; 4. Character constant area--the constant string is placed with this, the program is released at the end of the system; 5. program code area int a=0; Global initialization zone char *P1; Global uninitialized zone void Main () {int B; Stack char s[]= "BB"; Stack char *p2; Stack char *p3= "123"; Where, "123\0" constant area, p3 in the stack area * * * point to non-modifiable, with a const attribute; static int c=0; Global Zone p1= (char*) malloc (10); 10 byte area in the heap area strcpy (P1, "123"); "123\0" in the constant area, the compiler may be optimized to point to the same area as the P3} specific reference: http://blog.csdn.net/yujiaming493323/article/details/8765345
About strcpy Segment Errors