1

I can not understand the error I get from gcc for the following source:

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

static void variable_arguments2(int n, va_list *ap) {
    for(int i=0; i<n; i++) {
        printf("%d\n", va_arg(*ap, int));
    }
}

static void variable_arguments1(int n, va_list ap) {
    variable_arguments2(n, &ap);
}

static void variable_arguments(int n, ...) {
    va_list args;
    va_start( args, n );
    variable_arguments1(n, args);
}

int main(void) {
    variable_arguments(3, 32, 45, 67);
    return 0;
}

I compile with

$ gcc -Wall test.c -o test

The error is:

test.c: In function 'variable_arguments1':
test.c:11:25: warning: passing argument 2 of 'variable_arguments2' from incompatible pointer type [-Wincompatible-pointer-types]
  variable_arguments2(n, &ap);
                         ^
test.c:4:13: note: expected '__va_list_tag (*)[1]' but argument is of type '__va_list_tag **'
 static void variable_arguments2(int n, va_list *ap) {
             ^~~~~~~~~~~~~~~~~~~

the variable_arguments2() function accepts pointer to va_list as argument and within the variable_arguments1() function I pass exactly this, or at least I think I do.
So I don't understand the error message at all.
If I change the function variable_arguments1() as follows

static void variable_arguments1(int n, va_list ap) {
    va_list args;
    va_copy(args,ap);
    variable_arguments2(n, &args);
}

gcc no longer reports any errors or warnings and the executable works as expected.
But I wonder why my first way doesn't compile. And then I wonder if there is another way to pass a pointer to va_list without having to create a copy.

mastupristi
  • 1,240
  • 1
  • 13
  • 29

0 Answers0