Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove this warning: second parameter of ‘va_start’ not last named argument?

Tags:

c

linux

gcc

I have a function (see below) that is emitting the following warning:

second parameter of ‘va_start’ not last named argument

What does it means and how to remove it?

The function is as the following:

static int  ui_show_warning(GtkWindow *parent, const gchar *fmt, size_t size, ...)
    {
      GtkWidget *dialog = NULL;
      va_list args = NULL;
      int count = -1;
      char *msg = NULL;

      if((msg = malloc(size + 1)) == NULL)
        return -12;

      va_start(args, fmt);

      if((count = snprintf(msg, size, fmt, args)) < 0)
        goto outer;

      dialog = gtk_message_dialog_new(parent,
                      GTK_DIALOG_DESTROY_WITH_PARENT,
                      GTK_MESSAGE_WARNING,
                      GTK_BUTTONS_OK,
                      "%s", msg);
      (void) gtk_dialog_run(GTK_DIALOG(dialog));

      gtk_widget_destroy(dialog);

     outer: {
        if(args != NULL)
          va_end(args);

        if(msg != NULL)
          free(msg);

        return count;
      }
    }
like image 956
Jack Avatar asked Nov 02 '12 04:11

Jack


3 Answers

You need to use size instead of fmt:

va_start(args, size);

It is size, not fmt, that is the last parameter that has an explicit name (as opposed to vararg parameters, which have no names). You need to pass the last named parameter to va_start in order for it to figure out the address in memory at which the vararg parameters start.

like image 162
Sergey Kalinichenko Avatar answered Nov 17 '22 05:11

Sergey Kalinichenko


second parameter of ‘va_start’ not last named argument

What does it means and how to remove it?

Your function has named parameters parent, fmt and size. The C spec says you have to always pass the last named parameter to va_start, for compatibility with older compilers. So you must pass size, not fmt.

(But with a modern compiler, it might work anyway)

like image 37
user9876 Avatar answered Nov 17 '22 05:11

user9876


I think there is a confusion here: most of people only deal with prinf-like functionsh which have format and varargs. and they think they have to pass parameter name which describes format. however va_start has nothing to do with any kind of printf like format. this is just a function which calculates offset on the stack where unnamed parameters start.

like image 5
mishmashru Avatar answered Nov 17 '22 04:11

mishmashru