gpt4 book ai didi

c - 具有最小参数的可变参数函数

转载 作者:太空宇宙 更新时间:2023-11-04 02:10:59 25 4
gpt4 key购买 nike

<分区>

Possible Duplicates:
C++ preprocessor __VA_ARGS__ number of arguments
How to count the number of arguments passed to a function that accepts a variable number of arguments?

为了学习可变参数函数,我写了一个demo:

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

int foo(int n, int m, ...);

int
main (int argc, char *argv[])
{
int n = 10;
int m = 15;
int p = 20;
//foo (n, m);
foo(n, m, 20);

return EXIT_SUCCESS;
}

int foo (int n, int m, ...)
{
va_list ap;
int p;
char *str;

va_start (ap, m);
p = va_arg (ap, int);
n = m + p;
printf ("%d.\n", n);
va_end (ap);
return 0;
}

我想知道如何处理只有两个参数的函数。 (对于这个demo,如果只有nm,运行foo后,我想得到结果:n = n+m 。)

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