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.
test_variadicthan inmain, so when we are answering your question we can just sayarather than "main's a" versus "test_variadic's a" (which are different variables)