I have a problem with using va_list. The below code works for an int:
main() {
int f1=1;
float** m = function(n,f1);
}
float** function(int n,...) {
va_list mem_list;
va_start(mem_list, n);
for (int i=0;i<n;i++) {
for (int j=0;j<n;j++) {
//m[i][j]=va_arg(mem_list,float);
int f = va_arg(mem_list,int);
printf("%i \n",f);
}
}
va_end(mem_list);
return NULL;
}
However when I change to a float i.e.
float f1=1.0;
float f = va_arg(mem_list,float);
printf("%f \n",f);
It does not return the right value (the value is 0.00000). I am extremely confused at what is happening.
In the context of a varargs call, float is actually promoted to double. Thus, you'll need to actually be pulling doubles out, not floats. This is the same reason that %f for printf() works with both float and double.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With