简体   繁体   English

转发可变参数列表到ncurses c中的printw函数

[英]Forward Variadic Argument List to ncurses printw function in c

This has been asked in several different flavors. 有人以几种不同的口味要求了这一点。 Yet I can still not get it to work. 但是我仍然无法使它正常工作。 Here is my function definition. 这是我的函数定义。

void                                                                           
ncurses_add_line(const char *fmt, ...)                                         
{                                                                              
  if (ncurses_window) {                                                        
    va_list args;                                                              
    va_start(args, fmt);                                                       
    printw(fmt, args);                                                           
    printw("\n");                                         
    va_end(args);                                                              
  }                                                                            
}

When I call this function I get gibberish in the variadic print out of my function. 当我调用此函数时,我的函数在可变参数打印中变得乱码。 If I call printw directly it all works. 如果我直接给printw打电话,那一切正常。 For example, if I call ncurses_add_line like ncurses_add_line("Hello %d", var) I get a value not store in var. 例如,如果我像ncurses_add_line("Hello %d", var)一样调用ncurses_add_line ,则会得到一个不存储在var中的值。 However, if I call printw("Hello %d", var) , I see the value of var displayed next to "Hello" as in, if var == 1 then "Hello 1" is printed with printw but this is not the case for ncurses_add_line . 但是,如果我调用printw("Hello %d", var) ,则看到var的值显示在“ Hello”旁边,如var == 1,则将printw打印“ Hello 1”,但这不是ncurses_add_line

What do I need to change? 我需要更改什么?

My reason for wrapping this up is because I don't want to include in my header file, only in my c file. 我总结一下的原因是因为我不想包含在头文件中,而只包含在c文件中。

Try vwprintw instead of printw . 尝试使用vwprintw而不是printw vwprintw takes a va_list as its argument. vwprintw将va_list作为其参数。

The idiom you're trying to use -- passing a va_list to a function that takes a variable number of arguments -- won't work. 您尝试使用的惯用法-将va_list传递给带有可变数量参数的函数-将不起作用。 One solution is to find a variant of the function that will work (in this case, vwprintw). 一种解决方案是找到可以使用的功能的变体(在本例中为vwprintw)。 An alternative is to "flatten" the va_list: in this case, you could use vsprintf to create a formatted string, and then pass that into curses. 另一种方法是“压平” va_list:在这种情况下,可以使用vsprintf创建格式化的字符串,然后将其传递给curses。

args is not something like an array of arguments. args 不同于参数数组。 It is an internal structure. 这是一个内部结构。 You have to read out every single argument by passing the type. 您必须通过传递类型来读出每个参数。 Please keep in mind, that in C there is no runtime reflection, so you have to add the types in your code. 请记住,在C语言中没有运行时反射,因此您必须在代码中添加类型。

void ncurses_add_line(const char *fmt, ...)                                         
{                                                                              
if (ncurses_window) 
{            
  va_list args; 
  va_start(args, fmt);       
  char *arg = va_arg( args, int ); // take out one arg by giving the type (int)
  printw(fmt, arg);   
  printw("\n");                                         
  va_end(args);                                                              
  }                                                                            
}

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

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