深入理解字符串常量
cpp
#include<stdio.h>
int main() {
char str1[] = "abcd";
char* str2 = "abcd";
printf("%s\n", str1);
printf("%s\n", str2);
return 0;
}
char str1[] = "abcd";是用字符串初始化数组。
char* str2 = "abcd"; "abcd"为字符串常量
字符串常量不能被修改
例如1:
cpp
#include<stdio.h>
int main(int argc,const char*argv[]) {
char* a[] = { "Work","at","huawei" };
char** p = a;
*(*(p + 2) + 1) = 'H';
printf("%c %c\n", a[2][1], *(*(p + 2) + 1));
return 0;
}
VS环境下没有输出,但linux下会报错
例如2: