c语言指针字符串与字符数组字符串的区别

#include <stdio.h>



int main() { //字符串常量,存放于内存常量区。 //常量区区的内存具有缓存机制, //当不同指针指向的常量值相同时, //其实这些指针指向的是同一块常量区内存 //且常量区内存不允许被程序修改

    char *str1 = "hello"; char *str2 = "hello"; //报错 // *(str1+2)='A';

 printf("str1 = %p\n",str1); printf("str2 = %p\n",str2); //字符串变量存放于栈内存中, //不同字符数组指向的字符串值相同, //也是保存在两块不相同的内存中 //且栈内存允许被程序修改

 char str3[] = "hello"; char str4[] = "hello"; //不报错 // str3[2]='A';

 printf("str3 = %p\n",str3); printf("str4 = %p\n",str4); return 0; }

结果:

str1 = 0x10f17df80

str2 = 0x10f17df80

str3 = 0x7fff50a82bf2

str4 = 0x7fff50a82bec

你可能感兴趣的:(字符串)