#include "stdio.h"
int main(void)
{
char *str = "hello world\r\n"; //指针类型初始化字符串,只读,
//"hello world\r\n"存储在常量区
char buf[] = "hello world\r\n"; //数组类型初始化字符串,读写
//"hello world\r\n"存储在全局区或栈区
printf("buf=%s\r",buf);//输出原字符串
buf[0] = 't'; //正确,数组内容可以修改
printf("buf=%s\r\n",buf);//输出修改后的字符串
printf("str=%s\r",str);//输出原字符串
str = "Test"; //正确,指针指向新的字符串
printf("str=%s",str);//输出修改后的字符串
*(str+0) = 't'; //错误,指针指向内容不可修改
printf("(str+0)=%s",str);//因为上一句错误,此处无法执行
return 0;
}
输出如图: