va_arg() question

Discussion in 'C Programming' started by cfrans, Jul 31, 2013.

  1. cfrans

    cfrans Guest

    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:

    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,

    cfrans, Jul 31, 2013
    1. Advertisements

  2. cfrans

    Siri Cruise Guest

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

    will pass A a pointer (float*).

    void f(int n,...) {
    va_list ap; va_start(ap,n);
    float *A = va_arg(ap,float*);
    for (int k=0; k<n; k++) A[k] = k;
    Siri Cruise, Jul 31, 2013
    1. Advertisements

  3. cfrans

    James Kuyper Guest

    Those conversions are unnecessary - they will occur automatically even
    if you don't specify them.
    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*);
    James Kuyper, Jul 31, 2013
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.