C / C ++的va_list没有返回正确的参数 [英] C/C++ va_list not returning arguments properly

查看:204
本文介绍了C / C ++的va_list没有返回正确的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用va_list的一个问题。下面code适用于一个int:

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);

它不会返回正确的值(该值为0.00000)。我在所发生的事情极其混乱。

It does not return the right value (the value is 0.00000). I am extremely confused at what is happening.

推荐答案

在一个可变参数调用的上下文,浮动实际上是晋升为。因此,你需要实际上是拉动双击取值出来的,不是浮动秒。这是%F 的printf一样的道理()可以使用两种浮动双击

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.

这篇关于C / C ++的va_list没有返回正确的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆