简体   繁体   English

可变参数加法函数C

[英]Variadic Adding Function C

I'm writing a simple variadic function that adds together a series of ints and returns the sum. 我正在编写一个简单的可变参数函数,该函数将一系列整数相加并返回总和。 I'm having a bit of trouble understanding how it works and my code doesn't seem to work though I feel I'm in the right direction with my code. 我在理解它的工作方式时遇到了一些麻烦,尽管我觉得我的代码方向正确,但我的代码似乎没有工作。 (Posted below) The specifications of this function are that it takes at least one param and the last param is always a zero (as seen called in the main). (在下面发布)此函数的规范是,它至少需要一个参数,而最后一个参数始终为零(如在主体中所看到的)。 I was also told, based upon my machine, that I wouldn't necessarily get the output I'm looking for which, as you could imagine, further complicates my situation. 我还被告知,基于我的机器,我不一定会得到我正在寻找的输出,正如您可以想象的那样,这会使我的情况更加复杂。 Some assistance with correcting my Sum() function would be greatly appreciated. 在纠正我的Sum()函数方面的一些帮助将不胜感激。

EDIT: This is supposed to be done w/o the use of stdarg.h header and thus no va_arg functions. 编辑:这应该是通过不使用stdarg.h标头完成的,因此没有va_arg函数。

int Sum(int a, ... ) {
   int sum = 0, *addy = &a;

   while (*addy) {
      sum += *addy;
      addy += sizeof(a);
   }

   return sum;
}


int main() {
   printf("%d %d %d %d\n", Sum(0), Sum(3, 5, 6, 7, 0),
   Sum(7, 2, 42, 3, 5, -4, 0), Sum(-1, 9, 12, 123, -213, 42, 7, 2, 0));
}
//Expected output: 0 21 55 -19
//My output: 0 32770 32770 32776

When you add a number to an int pointer (as in addy += sizeof(a) ) the number you add is automatically multiplied by the size of whatever type the pointer is declared as (in this case int ). 当您向int指针添加数字时(如addy += sizeof(a) ),您添加的数字会自动乘以该指针声明为的任何类型的大小(在本例中为int )。 To fix this, just use 要解决此问题,只需使用

addy += 1;

instead. 代替。 However, I would recommend using variadic macros instead of this method, they are clearer and less error prone. 但是,我建议您使用可变参数宏而不是此方法,因为它们更加清晰且不易出错。

for variable arguments, you have to use va_start and va_end functions, hope useful.. 对于变量参数,必须使用va_startva_end函数,希望有用。

http://www.gnu.org/software/libc/manual/html_node/Variadic-Example.html#Variadic-Example http://www.gnu.org/software/libc/manual/html_node/Variadic-Example.html#Variadic-Example

Can you please check this 你能检查一下吗

int Sum(int a, ... ) {
 int sum = 0, *addy = &a;

while (*addy) {
  sum += *addy;
  addy ++;
}

return sum;
}


int main() {
   printf("%d %d %d %d\n", Sum(0), Sum(3, 5, 6, 7, 0),
  Sum(7, 2, 42, 3, 5, -4, 0), Sum(-1, 9, 12, 123, -213, 42, 7, 2, 0));
}

Point to remember is for pointer operations: the number you are adding to the pointer will be multiplied by the size of the type that the pointer is pointing to. 要记住的一点是指针操作:要添加到指针的数字将乘以指针所指向的类型的大小。 So incrementing the pointer addy is enough for geting the next element. 因此,递增指针addy足以获取下一个元素。

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

 int
 add_em_up (int count,...)
 {
   va_list ap;
   int i, sum;

   va_start (ap, count);         /* Initialize the argument list. */

   sum = 0;
   for (i = 0; i < count; i++)
     sum += va_arg (ap, int);    /* Get the next argument value. */

   va_end (ap);                  /* Clean up. */
   return sum;
 }

 int
 main (void)
 {
   /* This call prints 16. */
   printf ("%d\n", add_em_up (3, 5, 5, 6));

   /* This call prints 55. */
   printf ("%d\n", add_em_up (10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10));

   return 0;
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM