代码如下
#include<iostream> #include<string> using namespace std ; int main( ) { //C风格 char s1[] = "abcd" ; char s2[] = {"a","b","c","d"} ;//不以空字符结束 cout<<strlen( s1 )<<endl ; cout<<"s1: "<<s1<<endl ; cout<<strlen( s2 )<<endl ;/*严重错误,s2没有以空字符结束, strlen函数可能沿着s2在内存中的位置一直寻找, 直到遇到空字符*/ cout<<"s2: "<<s2<<endl ; cout<<strcmp(s1,s2)<<endl ; string s("hello world") ; const char *s3 = s.c_str() ; cout<<"*s3: "<<*s3<<endl ; cout<<"s3: "<<s3<<endl ; int a[3]={1,2,3} ; cout<<"*a: "<<*a<<endl ; cout<<"a: "<<a <<endl ; return 0 ; }
解决方案
20
20