我想知道如何将 C 中的数组梳理为函数的几个参数。在我看到 Go (golang) 惊人的语法糖之后,我想到了它。
C代码:
#include <stdio.h>
#include <stdarg.h>
// assert: all args are valid ints
void 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 main
import "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...)”相同的效果?
达令说
猛跑小猪
相关分类