2010-01-14 9 views
1
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

int main() 
{ 
    char *a = "Hello "; 
    const char *b = "World"; 

    printf("%s", strcat(a, b)); 
    system("PAUSE"); 

    return EXIT_SUCCESS; 
} 
+3

문자열 리터럴은 수정할 수 없습니다. http://stackoverflow.com/questions/1614723/why-is-this-c-code-causing-a-segmentation-fault/1614739#1614739 – AnT

답변

7

, 당신이 할당되지 않은 메모리에 작성에 B을 연결하는 경우.

실제로 strcat를 실행할 때 문자열 a의 문자 바로 뒤에 문자열 b의 문자가 추가됩니다. 그러나 문자열 a 뒤에 메모리를 요구하지 않았습니다.

2

당신은 당신이 소유하지 않은 메모리 위치에 데이터를 작성하기 때문에 당신이

관련 문제