c++面试题4

来源:互联网 发布:软件著作权部分转让 编辑:程序博客网 时间:2024/06/04 01:33

字符串的几个问题

#include<iostream>#include<string>#include<cstdio>using namespace std;int main(){string s1 = "haha";string s2 = "hehe";string s3 = s1 + s2;printf("%s\n",(s1+s2).c_str()); //没问题//char* p = (s1 + s2).c_str();//从类型‘const char*’到类型‘char*’的转换无效const char* p = (s1 + s2).c_str();printf("%s\n",p);char* p1="hahahehe";const char* p2 = p1; //char*类型转成const char* 没有问题printf("%s\n",p2);char sz1[100];printf("%d\n",sizeof(sz1)); //这个打印为100char* sz2 = "hahahaha";printf("%d\n",sizeof(sz2));//这个打印为4,注意指针一般使用strlen确定大小return 0;}


0 0