因为我想在函数中动态地查找数组的大小,所以我使用了sizeof运算符。但我得到了一些意想不到的结果 这里是一个演示程序,告诉你,我想要做什么。sizeof运算符的问题
//------------------------------------------------------------------------------------------
#include <iostream>
void getSize(int *S1){
int S_size = sizeof S1/sizeof(int);
std::cout<<"array size(in function):"<<S_size<<std::endl;
}
int main(){
int S[]={1,2,3,2,5,6,25,1,6,21,121,36,1,31,1,31,1,661,6};
getSize(S);
std::cout<<"array size:"<<sizeof S/sizeof(int)<<std::endl;
return 0;
}
//------------------------------------------------------------------------------------------
编译命令:克++ demo1.cc -o demo1的{的fedora 12}
输出:
array size(in function):2
array size:19
请解释,为什么发生这种情况。 可以做些什么来解决这个问题。
阅读[本](http://stackoverflow.com/questions/1975128/sizeof-an-array-in-the-c-programming-language/1975133#1975133)回答和[this](http:// stackoverflow.com/questions/2384107/magic-arguments-in-function-templates)问题。 – 2011-01-23 07:32:21
为什么数组不是指针的一个很好的例子。 – Oystein 2011-01-23 07:33:38