c - 动态分配多个结构的数组

标签 c arrays struct

我有一堆这样声明的结构:

typedef struct {
    int docid;
    int freq;
} pairs_t;

typedef struct {
    char *word;
    int numlines;
    pairs_t *pairs;
    int index;
    int npairs;
} data_t;

typedef struct {
    data_t *data;
    int numwords;
} index_t;

我想在 index_t 中创建一个结构数组,其中 index_t 将保存有关 data_t 中每个元素的信息。

我正在努力

  • index_t 中数组 data_t *data 的 malloc 空间,用于保存结构数组。
  • 在结构数组需要时重新分配更多空间。
  • 为结构数组中的每个元素分配足够的空间

我一直在玩这个,这就是我想出的:

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

int
main(int argc, char *argv[]) {
    int initialsize = 1;
    int count = 0;

    index_t *index;
    index->data = (data_t*)malloc(initialsize * sizeof(data_t));

    index->data = realloc(index->data, 2 * initialsize);

    index->data[count] = malloc(sizeof(data_t));

    count++;

    return 0;
}

我只是想知道为什么我对 index->​​data[count] 的分配会导致错误。这绝不是一个合适的程序,我只是想知道为什么它不起作用。在我尝试更大的程序之前,我只是想看看我是否可以让所有这三个步骤都起作用。

错误是:

错误:从类型“void *”分配给类型“data_t”时类型不兼容

如有任何帮助,我们将不胜感激。

最佳答案

why my mallocing of index->data[count] is causing an error

这是错误的,因为 index->​​data[count] 是类型 data_t 但不是 data_t* 所以它不能保存地址由 malloc()

返回

顺便说一句,你不需要强制转换 malloc() 的值,因为它返回 void* ,它被隐式转换为它被分配给的变量类型

除此之外,正如其他人所指出的,您没有初始化 index


这里有一个方法可以解决你的问题:

int initialsize = 1;
int count = 0;

index_t *index;

//allocating memory for index
index = malloc(sizeof(index_t));

//allocating memory for `data`
index->data = malloc(initialsize * sizeof(data_t));

int required_size = 7; //I chose 7 randomly

//reallocating memory for `data`
index->data = realloc(index->data, required_size * sizeof(data_t));

count++;

此外,如果您想为数据元素之一的 pairs_t *pairs; 成员分配内存,您可以这样做:

int required_size = 2;

index->data = malloc(required_size * sizeof(data_t));

//for first element of array
index->data[0].pairs = malloc(required_size * sizeof(pairs_t)); 
//you can even realloc
index->data[0].pairs = realloc(index->data[0].pairs, 3 * sizeof(pairs_t));

顺便说一下,不要忘记在程序结束时释放 malloced 数据


这是一个总结所有内容的示例程序:

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

typedef struct {
    int docid;
    int freq;
} pairs_t;

typedef struct {
    char *word;
    int numlines;
    pairs_t *pairs;
    int index;
    int npairs;
} data_t;

typedef struct {
    data_t *data;
    int numwords;
} index_t;

int main(int argc, char *argv[]) 
{
    int initialsize = 1;
    int count = 0;

    index_t *index;
    index = malloc(sizeof(index_t));

    int required_size = 1;

    index->data = malloc(required_size * sizeof(data_t));

    //for first element of array
    index->data[0].pairs = malloc(required_size * sizeof(pairs_t)); 
    //you can even realloc
    index->data[0].pairs = realloc(index->data[0].pairs, 1 * sizeof(pairs_t));

    //now you can access the members of pairs this way

    index->data[0].pairs[0].docid = 777;
    index->data[0].pairs[0].freq  = 777;

    printf("docid : %d\nfreq  : %d", index->data[0].pairs[0].docid, index->data[0].pairs[0].freq);

    free(index->data[0].pairs);
    free(index->data);
    free(index);

    count++;

    return 0;
}

输出:

docid : 777
freq  : 777

工作样本:https://ideone.com/Bg6aWa

关于c - 动态分配多个结构的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39896752/

相关文章:

C编程自动八进制解释

python - 如何在不使用 numpy 的情况下将 2D 列表展平为 1D?

c - 如何用 C 中的结构修复无限循环

php - PHP 与 C 中的箭头运算符

c - c中的子串位置

C语言: Copying strings one by one to an array

c++ - 编写一个将覆盖主函数数组中的值的函数

一行中的C++可确定数组输入

c++ - 在 50...60 个文件之间传递类对象

struct - 为什么将结构传递给当前包中带有文字结构参数的函数不同于另一个包中的函数?