va_arg() question

C

cfrans

I am trying to use va_arg to assign elements to a variable argument list using va_arg(). I understand that floats need to be converted to double:

eg:
Depth = (float) va_arg(ap, double);
Tair = (float) va_arg(ap, double);

However I want to supply an array of floats as an argument but I am not sure how:

Td[10] = (float) va_arg(ap, double);
(Which does not work properly)


Any assistance or suggested reference would be greatly appreciated.

Thank you for your time,

Chris
 
S

Siri Cruise

I am trying to use va_arg to assign elements to a variable argument list
using va_arg(). I understand that floats need to be converted to double:

eg:
Depth = (float) va_arg(ap, double);
Tair = (float) va_arg(ap, double);

However I want to supply an array of floats as an argument but I am not sure
how:

Td[10] = (float) va_arg(ap, double);
(Which does not work properly)


Any assistance or suggested reference would be greatly appreciated.

Thank you for your time,

Chris

void f(int n,...);
float A[10];
f(10,A);

will pass A a pointer (float*).

void f(int n,...) {
va_list ap; va_start(ap,n);
float *A = va_arg(ap,float*);
va_end(ap);
for (int k=0; k<n; k++) A[k] = k;
}
 
J

James Kuyper

I am trying to use va_arg to assign elements to a variable argument list using va_arg(). I understand that floats need to be converted to double:

eg:
Depth = (float) va_arg(ap, double);
Tair = (float) va_arg(ap, double);

Those conversions are unnecessary - they will occur automatically even
if you don't specify them.
However I want to supply an array of floats as an argument but I am not sure how:

Td[10] = (float) va_arg(ap, double);
(Which does not work properly)

You cannot pass arrays to functions in C. You can pass a pointer to the
first element of an array, and you can access the other elements of that
array using the same syntax as if the pointer were an array. If you pass
a pointer to your function, all you need to do to extract it is specify
the correct type.

float *Td = va_arg(ap, float*);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

va_arg question 5
Question for a REAL expert on casting double to float... 23
Infinite arrays in the CCL 8
malloc() and dinamic data question 4
varargs's doubt 9
va_list help 5
Re-using a 'va_list' 2
newbie question 5

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top