从 realloc() 函数创建函数

标签 c arrays function realloc

我想创建一个函数来重新分配 typedef struct 的二维数组

typedef struct hero_data{
    char name[254];
    char title[254];
    int encoding;
    int startstr;
    double incstr;
    int startdex;
    double incdex;
    int startintel;
    double incintel;
    int basemindmg,basemaxdmg;
    double bat;
    double basearmor;
    struct hero_data *next;
    struct hero_data *Class;
}hero;

typedef struct parameters{ 
    int toughtotal;
    int nimbletotal;
    int smarttotal;
    int skeptictotal;
    int mystictotal;
    int cursedtotal;
    int brutetotal;
    int shreddertotal;
    int vanillatotal;
    int typetotal;
    int typenum;
    hero **smart[];
    hero **nimble[];
    hero **tough[]; 
    hero **type[][];
    hero **skeptic[][];
    hero **mystic[][];
    hero **cursed[][];
    hero **brute[][];
    hero **shredder[][];
    hero **vanilla[][];
}Parameters;

void reallocation(Parameters *p, int typenum,int typetotal)
{
    int i;

    p = realloc(p,sizeof(Parameters *) * typenum);
    for ( i = 0; i < typenum; i++)
    {
        p[i] = realloc(p[i],sizeof(Parameters) * typetotal);
    }
}

上面的函数应该这样调用:void reallocation(p->type,p->typenum,p->typetotal);

因此,通过正确替换函数的参数,我希望函数看起来像:

void reallocation(Parameters *p, int typenum,int typetotal)
{
    int i;

    p->type = realloc(p->type,sizeof(Parameters *) * p->typenum);
    for ( i = 0; i < p->typenum; i++)
    {
        p->type[i] = realloc(p->type[i],sizeof(Parameters) * p->typetotal);
    }
}

名为Parameters 的typedef 结构包含int typenumint typetotal 以及应通过realloc( )

当我尝试编译时,在 Tiny C (Windows) 中出现错误:*文件在 C 中。

  1. 错误:无法将“struct parameters”转换为“void *”

    (这见于 'p[i] = realloc(p[i],sizeof(Parameters) * typetotal')

谁能帮我重写这个函数,以便我能够在 Parameter *p 中重新分配二维数组?


我尝试将 void reallocation(Parameters *p, ...) 更改为 void reallocation(Parameters *p[], ...) 和错误 # 2成为与错误 #1 相同的消息,它出现在 p[i] = realloc (...);

=

最佳答案

您的代码的一个大问题是您将不相等的类型分配给彼此,并且您也没有检查 realloc 的结果。如果此调用失败,您将泄漏最初分配的内存。

假设你的结构看起来像

typedef struct {
    int typenum;
    int typetotal;
} Parameters;

Parameters *p;

p = malloc(10 * sizeof(*p));
if (p == NULL)
    printf("Allocatation of memory failed!\n");

要正确地重新分配到 20,您可以这样做

reallocate_p(&p, 20);

函数定义为

void reallocate_p(Parameters **p, int new_size)
{
    Parameters *temp;

    temp = realloc(*p, sizeof(*temp) * new_size);
    if (temp==NULL) {
        printf("Reallocatation of memory failed!\n");
        // Handle error        
    }

    *p = temp;

    return;
}

另请注意,我们不会强制转换 malloc()realloc() 的返回值。 至于为什么,看这个reference

关于从 realloc() 函数创建函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19119729/

相关文章:

c - 使用 setsockopt() 系统绑定(bind)设备的原始套接字在 Fedora 核心 6 (2.6.18-1.2798.fc6) 中不起作用

c - 如何让我的程序使用 BSD 打印其 github 版本号?

Java教科书: "the size of an array must be known at compile time"

我们可以在 C 语言中声明一个函数和变量吗?

c++ - 如何将通用函数类型放入 std::map?

c - 有多少个由 N 位数字组成的数字,其和为 S? (动态规划)

c - 为什么 fread 返回零以及文件何时包含内容

php - 使用 PHP 对 MySQL 表中特定字段的总值进行求和,并以数组形式提取整行

javascript - 如何检查包含子数组的数组的大小?

python - 使用 If 条件时 Python 中的 NoneType 错误