Conclusion
一般人很少分辨char s[]和char *s的差异,大部分状况下用法相同,但char *s速度略快,因为不需copy的动作,且*s++为C语言常用的写法。
#include
#include
using namespace std;
int main()
{
char s1[] = "wangshihui";
char *s2 = "wangshihui";
cout << "size of s1: " << sizeof(s1) << endl;
cout << "size of s2: " << sizeof(s2) << endl;
cout<
for(int i = 0; i != sizeof(s1)-1; ++i)
{
cout << s1[i];
}
cout<
for(int i = 0; i != strlen(s2); ++i)
{
cout << *(s2 + i);
}
cout<
while(*s2)
cout << *s2++;
cout<
/************
while(*s1)//编译不通过:lvalue required as increment operand|
cout << *s1++;
*************/
}
/************************
size of s1: 11
size of s2: 4
----------
wangshihui
----------
wangshihui
----------
wangshihui
----------
**************************/