gpt4 book ai didi

c - 程序出了什么问题?

转载 作者:行者123 更新时间:2023-11-30 19:11:05 24 4
gpt4 key购买 nike

程序迷恋,请帮助我,先谢谢了。该文件可以在下面下载。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
int deed_id;
char deed_name[55];
int points_for_deed;
int total_times_done;
int total_points_earned;
} deed;

int main(){
FILE *file1;
file1=fopen("deed_list.txt", "r");
if(file1==NULL){
printf("Can not open the file");
return 1;
}
int j;
fscanf(file1, "%i", &j);
deed **deed_list = (deed**)malloc(sizeof(deed)*j);
int i;
for(i=0; i<j; i++){
fscanf(file1, "%i %s %i", &deed_list[i]->deed_id, deed_list[i]->deed_name, &deed_list[i]->points_for_deed);
}
printf("%i",j);
fclose(file1);
return 0;
}

https://sst-csci.com/csci151/wp-content/uploads/deed_list.txt

最佳答案

deed  **deed_list = (deed**)malloc(sizeof(deed)*j);

您不希望使用指向指针的指针来存储deed数组,请更改为单个指针(并且不要强制转换malloc):

deed  *deed_list = malloc(sizeof(deed)*j);

不要忘记在最后调用 free(deed_list);

关于c - 程序出了什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40691572/

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