剑指offer字符串学习

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/abc15766228491/article/details/81490565
#include <iostream>
using namespace std;
int main() {
    char str1[]="hello";
    char str2[]="hello";
    char *str3="hello";
    char *str4="hello";
    if (str1==str2)
        cout<<"yes"<<endl;
    else
        cout<<"no"<<endl;
    if (str3==str4)
        cout<<"yes"<<endl;
    else
        cout<<"no"<<endl;
    return 0;
}
/*
 * output:
 no
 yes
 */

str1 和str2 是两个字符串数组,我们会为他们分配两个长度为6字节的空间,并把”hello”的内容分别复制到数组中去。这是两个初始化地址不同的数组,所以str1和str2的值也不相同,所以输出的第一行是”no”

str3和str4是两个指针,我们无需为他们分配内存以存储字符串内容,而只需要把他们指向”hello”在内存中的地址就可以了。由于”hello”是常量字符串,他在内存中只有一个拷贝,因此str3和str4指向的结果是相同的,输出的第二行是”yes”

猜你喜欢

转载自blog.csdn.net/abc15766228491/article/details/81490565
今日推荐