【发布时间】:2014-09-09 14:28:19
【问题描述】:
我的代码很简单:
#include <iostream>
using namespace std;
int test(int b[]){
cout<<sizeof(b)<<endl;
return 1;
}
int main(){
int a[] ={1,2,3};
cout<<sizeof(a)<<endl;
test(a);
system("pause");
}
这段代码的输出是:
12
4
这意味着当a[]作为参数传递给函数test()时,is已经恶化为int *,所以size(b)的输出是4,而不是12。所以,我的问题是,如何获得函数 test() 中 b[] 的实际长度?
【问题讨论】:
-
嗨,IIRC 你不能,因为函数参数是一个指针。如果你真的需要这个功能,你可以使用
std::array或std::vector
标签: c++ arrays parameters integer