#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<windows.h>
int main() {
char str1[] = "hello world";
char str2[] = "hello world";
char *str3 = "hello world";
char *str4 = "hello world";
if (str1 == str2) {
printf("str1 and str2 are same\n");
}
else {
printf("str1 and str2 are not same\n");
}
if (str3 == str4) {
printf("str3 and str4 are same\n");
}
else {
printf("str3 and str4 are not same\n");
}
system("pause");
return 0;
}
输出结果:
当几个指针指向同一个字符串时,实际上它们指的是同一块内存,所以str3和str4是相同的;
但用相同的常量字符串去初始化不同的数组时就会开辟不同的内存块,所以str1和str2是不相同的。