请写出以下代码的打印结果:
#include <iostream.h>
#include <string.h>
void main(void)
{
char str[13]="Hello world!";
char *pStr="Hello world!";
cout<<sizeof(str)<<endl;
cout<<sizeof(pStr)<<endl;
cout<<strlen(str)<<endl;
cout<<strlen(pStr)<<endl;
return;
}
【答案】
打印结果:
13
4
12 12
注意:一定要记得数组名并不是真正意义上的指针,它的内涵要比指针丰富的多。但是当数组名当做参数传递给函数后,其失去原来的含义,变作普通的指针。另外要注意 sizeof 不是函数,只是操作符。