user1422573
user1422573

Reputation: 41

C/C++ va_list not returning arguments properly

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.

Upvotes: 4

Views: 1473

Answers (2)

Matthieu M.
Matthieu M.

Reputation: 300349

Since we are talking C++ here, you might want to use newer C++11 facilities.

I could suggest variadic templates, but it might be a bit too advanced, on the other hand to pass an arbitrary long list there now is std::initializer_list<T> where T is a simple type.

 void function(std::initializer_list<int> list);

It does not have many functions, just:

  • size() which returns how many elements are in the list
  • and begin() and end() which return iterators as usual

So you can actually do:

void function(std::initializer_list<int> list) {
    std::cout << "Gonna print " << list.size() << " integers:\n";

    bool first = true;
    for (int i: list) {
        if (first) { first = false; } else { std::cout << ", "; }
        std::cout << i;
    }
    std::cout << "\n";
}

And then invoke it as:

int main() {
    function({1, 2, 3, 4, 5});
}

which will print:

Gonna print 5 integers:
1, 2, 3, 4, 5

In general in C++, stay away from the C-variadic. They are type-unsafe and full of gotchas.

Upvotes: 3

FatalError
FatalError

Reputation: 54621

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.

Upvotes: 8

Related Questions