c - C 中的参数变量

标签 c variables arguments

我正在编写一个方法,它接受数字 n 和 n 个整数(变量),并且该函数将返回不包括 n 的整数之和。我被困在如何单独访问每个参数上。这是我到目前为止所得到的,我在网上读到了它,希望我走在正确的道路上。 在网上找到的似乎有用的方法是:

va_start()
va_arg()
va_end()


int sumv(int n, ...)
{
  va_list list;
  int sum = 0;
  while(n>0)
  {
    //*********************
    //this is the part where I am stuck on, how do I get each paramater?
    //I know it will be an int
    //*********************
    n--;
  }
  return sum;
}

最佳答案

它应该看起来像这样:

int sumv(int n, ...)
{
  va_list list;
  va_start(list, n);
  int sum = 0;
  while(n>0)
  {
    sum += va_arg(list, int);
    n--;
  }
  va_end(list);
  return sum;
}

关于c - C 中的参数变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16270686/

相关文章:

c++ - 在 2Darrays C++ 中手动设置值

php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP

c++ - C++ 或 C 中的 foo(void) 和 foo() 有区别吗?

Javascript 函数不接受变量作为参数

c - 在双指针中存储字符串时出现问题

c - 两种方式遍历INT数组

ruby-on-rails - 在什么情况下我应该使用实例变量而不是其他变量类型?

bash - 并行 : How to reference multiple arguments from a function

c - 在某些平台上向文件写入字节会被交换

c - 如何在不违反严格的别名规则的情况下合法地使用带 union 的类型双关语在 struct sockaddr 的变体之间进行转换?