将动态数组结构传递给函数进行分配

时间:2011-12-10 23:56:50

标签: c

必须创建一个struct数组。由于不知道数组中将有多少条目,因此必须使用动态数组。

这里没有按照建议工作 - https://stackoverflow.com/a/8455125/1090944

将struct数组传递给函数进行分配时遇到麻烦。

以下是代码:

#include<stdio.h>
#include<stdlib.h>


struct record
{
    char * community_name;
    double data[10];
    double crimes_per_pop;
};

void allocate_struct_array(struct record * array, int length);

int main()
{
    int num_lines = 10;
    struct record ** data_array;

    allocate_struct_array(&data_array, num_lines);

    data_array[0]->community_name[0] = 'h';
    printf("%c\n", data_array[0]->community_name[0]);

    return 0;
}


void allocate_struct_array(struct record * array, int length)
{
    int i;

    *array = malloc(length * sizeof(struct record *));

    if (!array)
    {
        fprintf(stderr, "Could not allocate the array of struct record *\n");
        exit(1);
    }

    for (i = 0; i < length; i++)
    {
        array[i] = malloc( sizeof(struct record) );

        if (!array[i])
        {
            fprintf(stderr, "Could not allocate array[%d]\n", i);
            exit(1);
        }

        array[i]->community_name = malloc(100 * sizeof(char));
    }
}

以下是我从编译器获得的错误和警告:

../src/temp.c: In function 'main':
../src/temp.c:19: warning: passing argument 1 of 'allocate_struct_array' from incompatible pointer type
../src/temp.c: In function 'allocate_struct_array':
../src/temp.c:32: error: incompatible types in assignment
../src/temp.c:42: error: incompatible types in assignment
../src/temp.c:44: error: wrong type argument to unary exclamation mark
../src/temp.c:50: error: invalid type argument of '->'
make: *** [src/temp.o] Error 1

2 个答案:

答案 0 :(得分:3)

我认为你错过了原答案的正确性,所以我在这里详细说明......

#include<stdio.h>
#include<stdlib.h>


struct record
{
    char * community_name;
    double data[10];
    double crimes_per_pop;
};

void allocate_struct_array(struct record ** array_pointer, int length);
/* the allocate_struct_array should take a POINTER to a POINTER */

int main()
{
    int num_lines = 10;
/* this is wrong:   struct record ** data_array; */
/* it should be: */ struct record *data_array; 

    allocate_struct_array(&data_array, num_lines); /* NOW this will work */

    data_array[0]->community_name[0] = 'h'; /* THIS WILL STILL CRASH community_name is not allocated!!! */
    printf("%c\n", data_array[0]->community_name[0]);

    return 0;
}


void allocate_struct_array(struct record **array_pointer, int length)
{
    int i;
    struct record *array;
    array = malloc(length * sizeof(struct record *));

    if (!array)
    {
        fprintf(stderr, "Could not allocate the array of struct record *\n");
        exit(1);
    }

    for (i = 0; i < length; i++)
    {
        array[i] = malloc( sizeof(struct record) );

        if (!array[i])
        {
            fprintf(stderr, "Could not allocate array[%d]\n", i);
            exit(1);
        }
    }
    *array_pointer = array; /* copied over. */
}

顺便说一下,我想向您展示一个结构分配记录的良好C实现:

答案 1 :(得分:1)

您的签名已被换掉。在main函数中,data_array应声明为struct record* array,只有一个*。您的allocate_struct_array函数应该使用struct record** array,并使用两个*

每个*是另一个间接层。你需要从main函数中只有一层间接开始(它是间接的,因为你指的是带有一个变量的多个结构)。

然后在allocate_struct_array函数中添加另一层间接,因为您需要从另一个函数修改变量。在调用allocate_struct_array时使用&运算符时,可以看到这种间接添加。