ITeye博客

字符串指针、字符数组的sizeof和strlen结果

standalone · 2010-09-27 08:36 · c++ · 编程语言 · Markdown

int main(){
	
	char *p1="china";
	char p2[]="china";
	char p3[100]={0};
	strcpy(p3, "china");
	cout << sizeof(p1) << endl;
	cout << sizeof(p2) << endl;
	cout << sizeof(p3) << endl;
	cout << strlen(p1) << endl;
	cout << strlen(p2) << endl;
	cout << strlen(p3) << endl;
}

 在vc上面的输出结果:

 

4

6

100

5

5

5

 

解释:sizeof(p1)是一个指针的大小;sizeof(p2)和sizeof(p3)是数组的大小,包括'\0',strlen则不包括结尾的'\0'。

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。