作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我如何在C中的用户定义函数中传递任意数量的参数?该函数的原型(prototype)是什么?它类似于可以接受任意数量参数的printf。
最佳答案
看here举个例子。
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
int maxof(int, ...) ;
void f(void);
main(){
f();
exit(EXIT SUCCESS);
}
int maxof(int n args, ...){
register int i;
int max, a;
va_list ap;
va_start(ap, n args);
max = va_arg(ap, int);
for(i = 2; i <= n_args; i++) {
if((a = va_arg(ap, int)) > max)
max = a;
}
va_end(ap);
return max;
}
void f(void) {
int i = 5;
int j[256];
j[42] = 24;
printf("%d\n",maxof(3, i, j[42], 0));
}
关于c - 如何在 C 中的用户定义函数中传递任意数量的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1177773/
我是一名优秀的程序员,十分优秀!