#include <stdio.h>
int main(){
char str1[] = "hello world";
char str2[] = "hello world";
char *str3 = "hello world";
char *str4 = "hello world";
if(str1 == str2)
printf("str1 == str2 \n");
else
printf("str1 != str2 \n");
if(str3 == str4)
printf("str3 == str4 \n");
else
printf("str3 != str4 \n");
printf("str1 = %p, str2 = %p\n",str1,str2);
printf("str3 = %p, str4 = %p\n",str3,str4);
}
注意:
1.数组分配空间方式
str1和str2数组分别分配12个字节空间,并将两个"hello world"字符串分别赋值到数组中,这是两个初始地址不同的数组。(str1 != str2)
2.指针分配空间方式
str3和str4两个指针,无需分配内存存放字符串内容,只需指向"hello world"所在的内存中的地址就可以了。(str3 == str4)