C 如何在函数中重新分配结构数组

C 如何在函数中重新分配结构数组,c,struct,memory-management,malloc,realloc,C,Struct,Memory Management,Malloc,Realloc,我正在尝试为我的学校项目分配一个动态的国家对象数组。我在main()函数中对数组进行了malloc,并在add\u country()函数中重新分配它。但这似乎给了我一个真实的错误。有人能帮忙吗?这是最小的可复制代码 #include <stdio.h> #include <string.h> #include <stdlib.h> int count = 0; typedef struct test_Country

我正在尝试为我的学校项目分配一个动态的国家对象数组。我在
main()
函数中对数组进行了malloc,并在
add\u country()
函数中重新分配它。但这似乎给了我一个真实的错误。有人能帮忙吗?这是最小的可复制代码

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    int count = 0;
    typedef struct test_Country
    {
    char name[20];
    int gold;
    int silver;
    int bronze;
    } test_Country;

    test_Country *addtest_Country(test_Country test_Country_obj, test_Country*array)
    {
    int flag = 0;
    printf("%s\n", "before realloc");
    test_Country *new_array;
    new_array = realloc(array, sizeof(test_Country *) * (count + 1));
    printf("%s\n", "after realloc");
    //array[count].name = (char *)malloc(strlen(test_Country_obj.name) + 1);
    if (count == 0)
    {
        strcpy(new_array[0].name, test_Country_obj.name);
    }
    else
    {

        for (int i = 0; i < count; i++)
        {

            if (strcasecmp(new_array[i].name, test_Country_obj.name) == 0)
            {
                printf("%s", "test_Country already added\n");
                flag = 1;
                break;
            }
        }
    }
    if (flag == 0)
    {
        strcpy(new_array[count].name, test_Country_obj.name);
        test_Country_obj.gold = 0;
        test_Country_obj.silver = 0;
        test_Country_obj.bronze = 0;
        new_array[count] = test_Country_obj;
        count = count + 1;
    }
    flag = 0;
    return new_array;
}

    int main()
    {
    char choice;
    test_Country *array = malloc(sizeof(test_Country));
    test_Country test_Country_obj;
    printf("%s", "Enter your choice : ");
    scanf("%s", &choice);
    //fgets(ptr, 80, stdin);
    //sscanf(ptr, "%c %s %d %d %d", &choice, test_Country_obj.name, &test_Country_obj.gold, &test_Country_obj.silver, &test_Country_obj.bronze);
    //printf("%s", &choice);
    while (choice != 'E')
    {

        printf("%s", "Enter test_Country name : ");
        scanf("%s", test_Country_obj.name);
        array = addtest_Country(test_Country_obj, array);
        //printf("%d%s", count, "is count");
        printf("%s", "Enter your choice : ");
        scanf("%s", &choice);
    }
    }
#包括
#包括
#包括
整数计数=0;
类型定义结构测试\u国家/地区
{
字符名[20];
国际黄金;
银币;
青铜;
}试验国;
测试国家/地区*添加测试国家/地区(测试国家/地区测试国家/地区obj,测试国家/地区*数组)
{
int标志=0;
printf(“%s\n”,“在realloc之前”);
测试_国家/地区*新_阵列;
新数组=realloc(数组,sizeof(test_Country*)*(count+1));
printf(“%s\n”,“在realloc之后”);
//数组[count].name=(char*)malloc(strlen(test\u Country\u obj.name)+1);
如果(计数=0)
{
strcpy(新数组[0]。名称,测试国家/地区对象名称);
}
其他的
{
for(int i=0;i
我似乎不明白怎么了

char choice;
scanf("%s", &choice);
这很糟糕<代码>选项
只能容纳一个字符,因此它只能容纳不超过零个字符的字符串。(一个字符的房间用于终止空字符)。试图存储长度超过零个字符的字符串会导致危险的超出范围写入,并可能破坏周围的数据

为了避免超出写入范围,您应该分配足够的元素并指定要读取的最大长度。最大长度应为缓冲区大小减去1以终止空字符

char choice[16]; /* allocate enough elements */
scanf("%15s", choice); /* specify the maximum length */
之后,
中的
选项
,而
开关
应替换为
选项[0]
,以根据第一个字符进行判断。另一种方法是使用
strcmp()
检查整个字符串

这很糟糕<代码>选项只能容纳一个字符,因此它只能容纳不超过零个字符的字符串。(一个字符的房间用于终止空字符)。试图存储长度超过零个字符的字符串会导致危险的超出范围写入,并可能破坏周围的数据

为了避免超出写入范围,您应该分配足够的元素并指定要读取的最大长度。最大长度应为缓冲区大小减去1以终止空字符

char choice[16]; /* allocate enough elements */
scanf("%15s", choice); /* specify the maximum length */

之后,
中的
选项
,而
开关
应替换为
选项[0]
,以根据第一个字符进行判断。另一种方法是使用
strcmp()
检查整个字符串。

它似乎给了我realloc invalid ponter错误。什么是“它”?请解释错误来自何处,并在文章中逐字提供。另外,请提供完整的代码作为参考。我得到了相同的错误。但我似乎不明白是什么原因造成的。我已经把导致问题的代码放在这里了。因此,当我选择“A”时,函数将转到add_country()函数。每次添加新国家/地区时,我都会尝试增加“阵列”的大小。country_obj.name获取国家名称作为输入。我添加了最小可复制代码。它似乎给了我realloc无效ponter错误。什么是“它”?请解释错误来自何处,并在文章中逐字提供。另外,请提供完整的代码作为参考。我得到了相同的错误。但我似乎不明白是什么原因造成的。我已经把导致问题的代码放在这里了。因此,当我选择“A”时,函数将转到add_country()函数。每次添加新国家/地区时,我都会尝试增加“阵列”的大小。country_obj.name获取国家名称作为输入。我添加了最小可复制代码。感谢您的建议,但我认为这不会导致问题,因为程序转到add_country函数并在realloc部分中断。@av1028调用未定义的行为并不总是导致崩溃。我不明白。你能再解释一下吗。我是编程新手。我添加了可复制的代码。还有,可能是我的系统出了问题吗?谢谢你的建议,但我不认为这是问题的原因,因为程序转到add_country函数,在realloc部分,它就坏了。@av1028调用未定义的行为并不总是导致崩溃。我不明白。你能再解释一下吗。我是编程新手。我添加了可复制的代码。还有,有没有可能是我的系统出了问题?