简体   繁体   English

如何返回浮点数组

[英]How to return an floating point array

I need a function that creates an array with some floating points. 我需要一个函数来创建带有一些浮点数的数组。

double * my_function( )
{
    static double arr[10] = {20, 21, 22, 23, 24, 25, 26, 27, 28, 29};

    return arr;
}


int main ()
{
    double *first_pos;
    int i;

    first_pos = my_function();
    for ( i = 0; i < 10; i++ )
    {
        printf( "%d", *(first_pos + i));
    }

return 0;
}

This prints some "random" numbers. 这将打印一些“随机”数字。

I'm a confused about pointers/arrays! 我对指针/数组感到困惑!

Your pointer/array usage is fine. 您的指针/数组用法很好。

printf("%f", *(p + i));

Print doubles with the %f specifier. 使用%f指示符将打印翻倍。 %d is for ints. %d用于整数。

I think it is %lf (long float) for doubles, and %f for normal floats 我认为双打是%lf(长浮点),普通浮点是%f

like this 像这样

  first_pos = my_function();
  for ( i = 0; i < 10; i++ ){
    printf( "%lf\n", *(first_pos + i));
  }

the output it gives me is 它给我的输出是

20.000000
21.000000
22.000000
23.000000
24.000000
25.000000
26.000000
27.000000
28.000000
29.000000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM