如何在函数之间传递数组

| 有人能帮我吗?我的C程序有问题。这里是: 在我的主讲中,我调用一个函数(func A),其中两个参数是第二个函数(fun B)和\“用户数据\”(原则上可以是单个数字,char或数组)。此“用户数据”也是函数B的参数。当“用户数据”是单个整数时,我可以正常工作,但现在我需要将其用作数组。所以目前的工作结构是这样的:
static int FunB(...,void *userdata_)  
{  
   int *a=userdata_;  
   ...  
   (here I use *a that in this case will be 47)  
   ...  
}  

int main()  
{  
   int b=47;  
   funcA(...,FunB,&b)  
}  
因此,现在我希望将b作为数组的主要对象(例如{3,45}),以便将一个以上的“数据”传递给函数B。 谢谢     
已邀请:
至少有两种方法可以做到这一点。 第一
static int FunB(..., void *userdata_)  
{  
   int *a = userdata_;  
   /* Here `a[0]` is 3, and `a[1]` is 45 */
   ...  
}  

int main()  
{  
   int b[] = { 3, 45 };  
   funcA(..., FunB, b); /* Note: `b`, not `&b` */
}  
第二
static int FunB(..., void *userdata_)  
{  
   int (*a)[2] = userdata_;  
   /* Here `(*a)[0]` is 3, and `(*a)[1]` is 45 */
   ...  
}  

int main()  
{  
   int b[] = { 3, 45 };  
   funcA(..., FunB, &b); /* Note: `&b`, not `b` */
}  
选择您更喜欢的一个。请注意,第二个变量是专门为数组大小固定且在编译时已知的情况(在这种情况下为
2
)量身定制的。在这种情况下,第二变体实际上是优选的。 如果数组大小不固定,则必须使用第一个变量。当然,您必须以某种方式将该大小传递给
FunB
。 注意数组如何传递给
funcA
(作为
b
&b
),以及如何在两种形式中的
FunB
(作为
a[i]
或as10ѭ)中进行访问。如果您未能正确执行,则代码可能会编译但无法正常工作。     

要回复问题请先登录注册