C ++数组大小 [英] C++ Size of Array
问题描述
可能重复:结果
的sizeof数组作为参数传递
块引用>我太傻了,在C ++这个sizeof操作符,你有什么想法,为什么是4和12?
void函数(INT ARG []){
COUT<<的sizeof(ARG)LT;< ENDL; // 4
} 诠释的main()
{
int数组[] = {1,2,3};
COUT<< sizeof的阵列<< ENDL; // 12
功能(数组);
返回0;
}
解决方案在
主
,名称阵列
是一个数组,所以你得到的数组的字节大小的sizeof
。然而,一个数组衰变时传递给一个函数指针,这样就可以获得的sizeof(INT *)
里面的功能。请注意,以参数的形式
T ARG []
是的究竟的一样走的是参数为T * ARG
。所以,你的功能是完全一样的对应void函数(INT * ARG){
COUT<<的sizeof(ARG)LT;< ENDL;
}Possible Duplicate:
Sizeof array passed as parameterI am being stupid with this sizeof operator in c++, do you have any idea why it is 4 and 12 ?
void function (int arg[]) { cout<<sizeof(arg)<<endl; // 4 } int main () { int array[] = {1, 2, 3}; cout<<sizeof array<<endl; // 12 function (array); return 0; }
解决方案In
main
, the namearray
is an array so you get the size in bytes of the array withsizeof
. However, an array decays to a pointer when passed to a function, so you getsizeof(int*)
inside the function.Be aware that taking an argument in the form of
T arg[]
is exactly the same as taking the argument asT* arg
. So your function is the exact equivalent ofvoid function(int* arg) { cout << sizeof(arg) << endl; }
这篇关于C ++数组大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!