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

时间:2022-09-06 11:18:51

Have to create an array of struct. Since don't know how many entries will have in the array, have to use dynamic array.

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

Did not worked as advised here - https://*.com/a/8455125/1090944.

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

Having troubles with passing array of struct to function for allocation.

传递struct数组来解决分配问题。

Here is the code:

这是代码:

#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));
    }
}

Here are errors and warnings that I got from the compiler:

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

../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 个解决方案

#1


3  

I think you missed the correctness of the original answer, so I will elaborate here...

我想你错过了原答案的正确性,所以我在这里详细阐述......

#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. */
}

By the way i would like to show you a good C implementation of structure allocations record:

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

#2


1  

Your signatures are swapped. In your main function data_array should be declared as a struct record* array,with only one *. Your allocate_struct_array function should instead take struct record** array, with two *'s.

您的签名已被交换。在main函数中,data_array应声明为struct record *数组,只有一个*。您的allocate_struct_array函数应该使用两个*来获取struct record **数组。

Each * is another layer of indirection. You need to start off with only one layer of indirection in the main function(it is indirect because you are referring to multiple structs with one variable).

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

You then add another layer of indirection in your allocate_struct_array function, as you need to modify a variable from another function. You can see this adding of the indirection when you use the & operator in your call of allocate_struct_array.

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

#1


3  

I think you missed the correctness of the original answer, so I will elaborate here...

我想你错过了原答案的正确性,所以我在这里详细阐述......

#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. */
}

By the way i would like to show you a good C implementation of structure allocations record:

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

#2


1  

Your signatures are swapped. In your main function data_array should be declared as a struct record* array,with only one *. Your allocate_struct_array function should instead take struct record** array, with two *'s.

您的签名已被交换。在main函数中,data_array应声明为struct record *数组,只有一个*。您的allocate_struct_array函数应该使用两个*来获取struct record **数组。

Each * is another layer of indirection. You need to start off with only one layer of indirection in the main function(it is indirect because you are referring to multiple structs with one variable).

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

You then add another layer of indirection in your allocate_struct_array function, as you need to modify a variable from another function. You can see this adding of the indirection when you use the & operator in your call of allocate_struct_array.

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