Passing an ellipsis to another variadic function [duplicate]

牧云@^-^@ 提交于 2019-11-27 11:38:01

You can't, you can only pass the arguments as a va_list. See the comp.lang.c FAQ.

In general, if you're writing variadic functions (that is, functions which take a variable number of arguments) in C, you should write two versions of each function: one which takes an ellipsis (...), and one which takes a va_list. The version taking an ellipsis should call va_start, call the version taking a va_list, call va_end, and return. There's no need for code duplication between the two versions of the function, since one calls the other.

Probably you can use variadic macros - like this:

#define FOO(...)  do { do_some_checks; myfun(__VA_ARGS__); } while (0)

NB! Variadic macros are C99-only

I don't know if this will help, you can access the variables by reference. This is kind of a sneaky trick, but it unfortunately won't allow you to use ellipsis in the final function definition.

#include <stdio.h>

void print_vars(int *n)
{
  int i;
  for(i=0;i<=*n;i++)
    printf("%X %d  ", (int)(n+i), *(n+i));
  printf("\n");
}

void pass_vars(int n, ...)
{
  print_vars(&n);
}

int main()
{
    pass_vars(4, 6, 7, 8, 0);
    return 0;
}

On my pc it outputs

$ ./a.out
BFFEB0B0 4  BFFEB0B4 6  BFFEB0B8 7  BFFEB0BC 8  BFFEB0C0 0

You have to pass va_list to the helper.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!