Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带两个指针的C函数_C_Function_Pointers_Reference - Fatal编程技术网

带两个指针的C函数

带两个指针的C函数,c,function,pointers,reference,C,Function,Pointers,Reference,你好,我有一个函数的问题。宣言是: int load_clusters(char *filename, struct cluster_t **arr) 在**arr之前,我不知道如何使用这两个指针。 我希望我这样称呼它: struct cluster_t *clusters; load_clusters("file.txt",&clusters); 但我不确定这是否正确 在函数中,我需要为它分配内存。 我想一定是这样 arr = (struct cluster_t**)malloc

你好,我有一个函数的问题。宣言是:

int load_clusters(char *filename, struct cluster_t **arr)
在**arr之前,我不知道如何使用这两个指针。 我希望我这样称呼它:

struct cluster_t *clusters;
load_clusters("file.txt",&clusters);
但我不确定这是否正确

在函数中,我需要为它分配内存。 我想一定是这样

 arr = (struct cluster_t**)malloc(count * sizeof(struct cluster_t*));
 arr[0...x] = (struct cluster_t*)malloc(sizeof(struct cluster_t));
 arr[0...x]->size += 1;
.
.
.
但在所有这些之后,我需要调用函数来打印集群

 void print_clusters(struct cluster_t *carr, int narr)
{
    printf("Clusters:\n");
    for (int i = 0; i < narr; i++)
    {
        printf("cluster %d: ", i);
        print_cluster(&carr[i]); AND THIS DOESN'T WORK AS I EXPECT
    }
}
void打印集群(结构集群,内部)
{
printf(“集群:\n”);
对于(int i=0;i

谢谢你的帮助;-)

在函数
load_clusters
中,
arr
是一个局部变量,因此对它所做的任何更改都不会反映在调用者中

您拥有的是要分配给的指针变量的地址。因此,取消对它的引用并为
struct cluster\t
的数组分配空间

*arr = malloc(count * sizeof(struct cluster_t));

另外,.

通常,**意味着函数将分配内存,并希望返回指向已分配内存的指针,因此需要传递指向指针的指针。