【发布时间】:2014-03-30 04:02:52
【问题描述】:
// 将 void x 的元素转换为整数数组
int elem(void* x,int size){
int* temp = new temp[size];
for(int i = 0;i < size ; i++){
temp[i] = (int*)x[i]; // <--- this is what i want
// assign all element of x to temp
}
}
int main(){
int a[] = {1,2,3,4,5,6,7,8,9,10};
int num = elem(&a,10);
}
【问题讨论】:
-
您确定需要
void *吗?elem是做什么的? -
elem 返回x的元素个数
-
改用模板。否则,将其设为 int*。
-
没错。模板可以很好地获取数组的大小。
void *没有,因为您必须传递大小才能知道。当然,你也可以使用std::array,直接使用.size()。 -
这不是我关心的问题。我只需要将 x 的所有元素转换为 temp 数组并返回 temp 的元素数。