C/C++ va_list not returning arguments properly

前提是你 提交于 2019-12-21 05:28:09

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/10871235/c-c-va-list-not-returning-arguments-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!