C 如何在函数中传递指向数组的指针、修改它并正确返回?

C 如何在函数中传递指向数组的指针、修改它并正确返回?,c,pointers,pass-by-reference,C,Pointers,Pass By Reference,我试图在函数中传递一个指向数组的指针并返回它。问题是,在正确初始化后,函数返回空指针。谁能告诉我,我的逻辑有什么问题 这是我的函数,其中声明了数组: void main() { int errCode; float *pol1, *pol2; pol1 = pol2 = NULL; errCode = inputPol("A", pol1); if (errCode != 0) { return; }

我试图在函数中传递一个指向数组的指针并返回它。问题是,在正确初始化后,函数返回空指针。谁能告诉我,我的逻辑有什么问题

这是我的函数,其中声明了数组:

void main()
{
     int errCode;
     float *pol1, *pol2;
     pol1 = pol2 = NULL;
     errCode = inputPol("A", pol1);
     if (errCode != 0)
     { 
         return;
     }

     // using pol1 array

     c = getchar();
}
下面是带有初始化的函数:

int inputPol(char* c, float *pol)
{
    pol= (float *) calloc(13, sizeof( float ) );
    while( TRUE )
    {
         // While smth happens
         pol[i] = 42;
         i++;
    };
}

您需要传递pol1的地址,以便main知道分配的内存在哪里:

void main()
{
    int errCode;
    float *pol1, *pol2;
    pol1 = pol2 = NULL;
    errCode = inputPol("A", &pol1);
    if (errCode != 0)
    { 
         return;
    }

    // using pol1 array

    c = getchar();
}

int inputPol(char* c, float **pol)
{
    *pol= (float *) calloc(13, sizeof( float ) );
    while( TRUE )
    {
         // While smth happens
         (*pol)[i] = 42;
         i++;
    };
}

您需要传递pol1的地址,以便main知道分配的内存在哪里:

void main()
{
    int errCode;
    float *pol1, *pol2;
    pol1 = pol2 = NULL;
    errCode = inputPol("A", &pol1);
    if (errCode != 0)
    { 
         return;
    }

    // using pol1 array

    c = getchar();
}

int inputPol(char* c, float **pol)
{
    *pol= (float *) calloc(13, sizeof( float ) );
    while( TRUE )
    {
         // While smth happens
         (*pol)[i] = 42;
         i++;
    };
}

您需要提高编译器警告级别(或注意警告),这样您就不会在没有
return
语句的情况下编写非空函数-/您发布的代码是否是您正在运行的完整代码?我在inputPol函数中看到了无限循环,您没有返回错误代码。您不需要在CY中强制转换
calloc
的结果。您需要提高编译器警告级别(或注意警告),这样您就不会在没有
return
语句的情况下编写非无效函数:-/您发布的代码是否是您正在运行的完整代码?我在inputPol函数中看到了无限循环,您没有返回错误代码。您不需要在C中强制转换
calloc
的结果