我想知道如何将 C 中的数组梳理为函数的几个参数。在我看到 Go (golang) 惊人的语法糖之后,我想到了它。C代码:#include <stdio.h>#include <stdarg.h>// assert: all args are valid intsvoid printEach(int len, ...) { // Irrelevant, this function print their arguments // And I know how to use va_start, va_arg...}void handleVet(int* v, int n) { // **HERE is my the question!!!** printEach(n, v[0]...[n]) // <----------- NON-C code. I need it.}int main(void) { int v[] = {12,14,15,15}; //I can do that only because the vector is static. I know the len when I'm coding printEach(4, v[0],v[1],v[2],v[3]); // But if we imagine an arbitrary vector, we got a problem handleVet(v, 4); return 0;}举例来说,在 go 中,它将是:package mainimport "fmt"func printEach (v ...int64) { // Irrelevant, this function print their arguments}func main() { var arr []int64 = []int64{1,14,15,} printEach(arr...)}如何在 C 中实现与“printEach(arr...)”相同的效果?
3 回答
达令说
TA贡献1821条经验 获得超6个赞
您将需要指定数组的大小。它可能如下所示:
void printEach(int* values, int size)
{
if(size==0)
return;
printf("%d", value[0]);
printEach(values+1, size-1);
}
- 3 回答
- 0 关注
- 203 浏览
添加回答
举报
0/150
提交
取消