如何返回浮点数组

Ulf*_*orm 0 c printf

我需要一个函数来创建一个带有一些浮点的数组.

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;
}
Run Code Online (Sandbox Code Playgroud)

这会打印一些"随机"数字.

我对指针/数组感到困惑!

Joh*_*ica 6

您的指针/数组使用情况很好.

printf("%f", *(p + i));
Run Code Online (Sandbox Code Playgroud)

使用说明%f符打印双打.%d是为了整数.