C ++简单的sizeof char数组和char指针之间的区别 [英] C++ simple sizeof difference between char array and char pointer
本文介绍了C ++简单的sizeof char数组和char指针之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
char * test = "test";
cout << sizeof(test);
char test2[] = "test";
cout << sizeof(test2);
在visual studio 2010上运行它,为什么输出 45
?
不应该是字符串文字, sizeof
字符串文字是字符串文字中的字符元素的数量,包括结束空字符?
Running this on visual studio 2010, why is the output 45
?
Shouldn't test be a string literal and the sizeof
a string literal be the number of character elements in the string literal including the terminating null character?
推荐答案
test
到字符串文字,而不是字符串文字( char []
):
-
sizeof(char *)
是4
,与/ code>
-
sizeof(char [5])
是5
,涉及test2 []
- the
sizeof(char*)
is4
, relating totest
- the
sizeof(char[5])
is5
, relating totest2[]
c> 45 是输出。
hence 45
is the output.
这篇关于C ++简单的sizeof char数组和char指针之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文