如何通过送价值数组在C发挥作用? [英] How to send array by value to function in c?
本文介绍了如何通过送价值数组在C发挥作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想是这样的:
typedef struct vec{
int sz;
int v[];
} ff;
int sum(struct vec z){
int o=0,i;
for(i=0;i<z.sz;i++)
o+=z.v[i];
return o;
}
int main(){
int test[]={10,1,2,3,4,5,6,7,8,9,10};
return sum((struct vec)test);
}
但这个例子code不能编译。如何通过送价值数组(不是REF抛指针)的功能?
But this example code can't compile. How to send array by value (not ref throw pointer) to function?
推荐答案
在你的榜样,你需要在结构VEC
例如INT v [10]。此外,您的初始化可以写越好。试试这个:
In your example, you will need to specify the exact size of the array in the definition of struct vec
, for example int v[10]. Also your initialization can be written better. Try this:
#define MAX_SIZE 50
struct vec {
int sz;
int v[MAX_SIZE];
};
int sum(struct vec z){
int i, o;
o = 0;
for(i=0; i<z.sz; i++) o += z.v[i];
return o;
}
int main(){
struct vec test = {10, {1,2,3,4,5,6,7,8,9,10}};
return sum(test);
}
这篇关于如何通过送价值数组在C发挥作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文