I am developing a C program and have been stumped by this warning. I want to retrieve arguments from the list using va_arg.
args[i] = (int) va_arg(argptr, int);
or
args[i] = (char) va_arg(argptr, char);
the problem that am getting this warning:
... void *' differs in levels of indirection from 'int'...
the same also for char case.
Any explanation for that?
code:
void test_function(va_list argptr, int (*callback)(),
int ret_typel)
{
int i ;
int arg_typel;
int no_moreb = TRUE;
void *args[MAX_FUNCTION_ARGS];
for (i=0; no_moreb; i++) {
arg_typel = (int)va_arg(argptr, int);
switch(arg_typel) {
case F_INT:
args[i] = (int) va_arg(argptr, int);
break;
case F_CHAR:
args[i] = (char) va_arg(argptr, char);
break;
default:
no_moreb = FALSE;
i--;
break;
}
}
}
The problem is that
args[]is an array ofvoid *. You cannot assign anintorfloatto avoid *(it doesn’t make any sense). You could get round this by casting, but it’s not a good idea.If you want to store different types in the same variable, consider a union:
UPDATE
As Jonathan Leffler correctly points out in his answer,
va_arg(argptr, char)andva_arg(argptr, float)should not be used, due to default promotions for variadic functions.