如何在程序在C中运行时更改数组的大小

时间:2018-05-09 12:05:22

标签: c memory ansi

好的,我正在尝试制作一个程序,列出一定数量的学生。在这里,您可以看到我询问用户的学生数量,并使用malloc来分配数组大小。

typedef struct{
    char name[MAXSTRING];
    int id;
} student;

int main()
{
    student *array;
    int ch, amount;

    printf("Give the amount of students\n");
    scanf("%d", &ch);

    array = (student*) malloc(amount*sizeof(student));

    //Some code, also includes free() for allocated memory
    return 0;
}

现在让我们说用户输入" 100"但随后他列出了100名学生,但随后他想再列出5名学生。我能做些什么来分配额外的5名学生。另外,如果每次超过限制我都可以一次又一次地分配。

提前谢谢。

0 个答案:

没有答案