gpt4 book ai didi

具有可能指向 malloc 分配字段的结构字段的常量指针数组?

转载 作者:行者123 更新时间:2023-11-30 18:06:21 25 4
gpt4 key购买 nike

int main() {    
Employee *array[SIZE]; //Employee is a typedef struct --includes char *name, DATE *dateOfBirth, DATE is also a typedef struct, has 3 int fields month, day, year,`

fillArray(array, &count, fpin1, fpin2);

freeMemory(array, int count);

}

fillArray(Employee *array[], int *count, FILE *fpin1, FILE *fpin2)
char buffer[MAX], buffer2[MAX];
while (fgets(buffer, MAX, fpin1) != NULL && fgets(buffer2, MAX, fpin2) != NULL){
array[*count]->name = (char *) malloc(sizeof(char)*25);
assert(array[*count]->name != NULL);
strncpy(array[*count]->name, buffer, 15);

strncpy(buffer2, temp, 2);
array[*count]->dateOfBirth->day = atoi(temp)
}

代码可以编译,但由于段错误而不断失败,它似乎在我的 fgets 上失败了?或者我的 malloc,我做错了什么?我实在是想不通。

此外,您将如何释放该内存

freeMemory(Employee *array[], int count)

功能?

最佳答案

应该是:

int main() {    
Employee array[SIZE]; //Employee is a typedef struct --includes char *name, DATE *dateOfBirth, DATE is also a typedef struct, has 3 int fields month, day, year,`

fillArray(&array, &count, fpin1, fpin2);

freeMemory(&array, int count);

}

您没有在任何地方分配 Employee 对象,因此 array[0] 指向某个随机地址。

关于具有可能指向 malloc 分配字段的结构字段的常量指针数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5607054/

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