0

Using this technique on a char * pointer has worked for me . (Not show here, because there is no error with it). However, I'm passing a pointer to a pointer to the test_variadic function.

Compiled with gcc -Wall -Wextra -Werror -Wpadded -std=c99 test.c -o test I try to extract it with va_arg(args, int**), Then I'll de-reference the pointer to assign a value to it and I get this error:

test.c: In function ‘test_variadic’:
test.c:14:6: error: assignment makes pointer from integer without a cast [-Werror]
   *aa = b++;    
      ^
cc1: all warnings being treated as errors

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

static int test_variadic(size_t length, ...)
{   
    int** aa;
    int b = 4;
    va_list args;
    va_start(args, length);

    for(size_t i = 0; i < length; i++) {
        aa = va_arg(args, int**);
        *aa = b++;              
    }

    va_end(args);

    return 0;
}

int main()
{
    int* a, *b;

    if(test_variadic(2, &a, &b) == 0) {
        printf("%d %d\n", *a, *b);
    }

    return 0;

}

So basically what I'm trying to do is pass the function pointers to variables (in this case ints), and then I want to assign those pointers to a value (from the function test_v*. I'd like to be able to pass pointers to the function, and then assign a value to the pointers. I want to be able to assign a value that is not a pointer so that I can access it back in main.

2
  • 3
    Dereferencing an int** leaves you with an int*, and b is an int (inside the function). Seems like you should be using int* arguments, making main's a and b into int's instead of int*'s Commented Apr 2, 2015 at 2:55
  • Suggestion: use different variable names in test_variadic than in main, so when we are answering your question we can just say a rather than "main's a" versus "test_variadic's a" (which are different variables) Commented Apr 2, 2015 at 3:00

1 Answer 1

1

The error is nothing to do with variadic lists; you are making a type mismatch. Your code is:

int **aa;   // a points to a pointer-to-int
int b;

// .... make 'a' point somewhere

*aa = b;   // error: (*aa) is a pointer-to-int, but you try to assign an int

Based on your post description, it seems you want to make *a point somewhere that will find 4.

Naively you might write *aa = &b; ++b; however that will fail because b is destroyed when the function returns. So you will have to allocate memory:

*aa = malloc( sizeof **aa );
if ( ! *aa )
    return 1;

**aa = b++;

The main function should then do free(a); free(b); after printing the values.

Sign up to request clarification or add additional context in comments.

3 Comments

going to accept your answer.. but I solved my own problem (lol). I can just pass a pointer to an int int i; test_variadic(&i); no need for an array of ints
Nobody suggested an array anyway... but glad you got it solved
by array (probably not an accurate description) I meant a pointer to a pointer int**

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.