converting va_list to variable argument? [duplicate]

家住魔仙堡 提交于 2019-12-25 02:46:12

问题


I have a function log_message it takes variable arguments.

log_message(int level, char *fmt, ...)

now before calling this(log_message) function i have to add new function(_log_message), and new function will call log_message.

_log_message(int level, char *fmt, ...)

new function is also same. when _log_message will call log_message it will convert variable input to va_list. Now i have va_list, i don't wanna change the original one, is there any way to change back to variable input, so i will able to call the original one(log_message).


回答1:


No, there is no way to turn a va_list back into a list of arguments.

The usual approach is to define a base function which takes a va_list as an argument. For example, the standard C library defines printf and vprintf; the first is a varargs function and the second has exactly the same functionality but takes a va_list instead. Similarly, it defines fprintf and vfprintf. It's trivial to define printf, vprintf and fprintf in terms of vfprintf:

int fprintf(FILE* stream, const char* format, ...) {
  va_list ap;
  va_start(ap, format);
  int n = vfprintf(stream, format, ap);
  va_end(ap);
  return n;
}

int vprintf(const char* format, va_list ap) {
  return vfprintf(stdout, format, ap);
}

int printf(const char* format, ...) {
  va_list ap;
  va_start(ap, format);
  int n = vprintf(format, ap);
  va_end(ap);
  return n;
}

(Similarly for the various exec* functions, which come in both va_list and varargs varieties.)

I'd suggest you adopt a similar strategy.



来源:https://stackoverflow.com/questions/24927094/converting-va-list-to-variable-argument

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