gpt4 book ai didi

c - 传递可变数量的参数

转载 作者:太空狗 更新时间:2023-10-29 16:52:25 25 4
gpt4 key购买 nike

我们可以将可变数量的参数传递给 c 中的函数吗?

最佳答案

Here是一个例子:

#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>

int maxof(int, ...) ;
void f(void);

int main(void){
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 - 传递可变数量的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3836272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com