这段代码为什么会导致运行时错误?

问题描述:

#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://*.com/questions/1614723/why-is-this-c-code-causing-a-segmentation-fault/1614739#1614739 – AnT 2010-01-14 08:19:22

因为你是在说你没有自己的存储位置写入数据。

事实上,在运行strcat时,会在字符串a的字符之后追加字符串b的字符。但是,在字符串a之后你还没有声明过内存。

当您串联B到A你正在写到内存中,你没有分配,