字符串复制的问题
#include<stdio.h>#include<string.h>
{
char *message="Original message";
strcpy(message,"Hello ");
strcat(message,"customer_name");
strcat(message,",how are you?");
printf("%s\n",message);
}
运行之后程序异常停止为什么?是字符串常量不能修改吗?
#include <stdio.h> #include <string.h> #include <stdlib.h> #define MAX 256 int main(void) { char *pstr = "Original message"; char *message = pstr; message = malloc(MAX * sizeof(char)); if (message != NULL) { strcpy(message, "Hello "); strcat(message, "customer_name"); strcat(message, ", how are you?"); printf("%s\n", message); } else { printf("malloc error!\n"); } return 0; }