c++ 中
常量字符串存储在单独额内存区域,
当其赋给指针时,指针指向字符串首地址
当将其存入数组,是将字符串的每个值依次存入数组,此时数组首地址与常量字符串首地址不同
//output
str1 and str2 are not same.
0
str1 and str2 are the same.
0
== 同 java,比较地址
strcmp 同 java equal, 比较内容
常量字符串存储在单独额内存区域,
当其赋给指针时,指针指向字符串首地址
当将其存入数组,是将字符串的每个值依次存入数组,此时数组首地址与常量字符串首地址不同
#include <stdio.h>
#include <string.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 the same.\n");
} else {
printf("str1 and str2 are not same.\n");
}
printf("%d\n", strcmp(str1, str2));
if(str3 == str4) {
printf("str3 and str4 are the same.\n");
} else {
printf("str3 and str4 are not same.\n");
}
printf("%d\n", strcmp(str3, str4));
return 0;
}
//output
str1 and str2 are not same.
0
str1 and str2 are the same.
0
== 同 java,比较地址
strcmp 同 java equal, 比较内容