gpt4 book ai didi

c - 这种重新分配方法是否正确

转载 作者:太空宇宙 更新时间:2023-11-04 03:19:37 26 4
gpt4 key购买 nike

<分区>

我的代码有效,但我的问题是这种动态分配是否正确。它运行良好,一切正常,但我不确定它是否正确。

        StudentDynamic* pStudents = NULL;
char auxfirstName[255], auxlastName[255];
float auxGrade;
FILE* pFile = fopen("InitialDB.txt", "r");
if (pFile == NULL)
{
printf("Could not open file or is empty! Exiting...");
exit(2);
}
int i = 0;
pStudents = (StudentDynamic*)malloc(sizeof(StudentDynamic) * 1);
while (!feof(pFile))
{
fscanf(pFile, "%s", auxfirstName);
pStudents[i].firstName = (char*)malloc(strlen(auxfirstName) + 1);
strcpy(pStudents[i].firstName, auxfirstName);

fscanf(pFile, "%s", auxlastName);
pStudents[i].lastName = (char*)malloc(strlen(auxlastName) + 1);
strcpy(pStudents[i].lastName, auxlastName);

fscanf(pFile, "%f", &auxGrade);
pStudents[i].grade = auxGrade;

i++;
pStudents = (StudentDynamic*)realloc(pStudents, sizeof(StudentDynamic) * (i + 1));
}
nStudents = i;
fclose(pFile);

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com