gpt4 book ai didi

c - 退出函数后,使用 realloc 不会将值存储在数组中

转载 作者:太空宇宙 更新时间:2023-11-03 23:33:55 24 4
gpt4 key购买 nike

将数据库中的值放入数组中并将值发送到函数本身内的 addReader 后,值已成功存储,但是当返回 main 时,输入的值消失了。

给定以下代码:使用 realloc 创建动态数组

reader* readerBuilder(reader *rdr, int *readNum){       //building all the readers
FILE *read;
int i=1;
char *str;
read = fopen("Readers.txt","r");
checkFile(read);
str = readFromFile(read);
while(str != NULL)
{
rdr[i-1] = *cutToRdr(str);
str = readFromFile(read);
if(str != NULL){
i++;
rdr = (reader*)realloc(rdr,sizeof(reader)*i);
checkreader(rdr);
}
}
fclose(read);
*readNum = i;
return rdr;
}

给定调用函数:

reader* addReader(reader *rdr, int *readNum){       //adding a reader from the user
char string[1000];
rdr = (reader*)realloc(rdr,sizeof(reader)*(*readNum+1));// build new struct array ( bigger), with the old values
checkreader(rdr);// check if can get memory
printf("please enter the I.D. of the student you want to add:\n");
scanf("%s",string);// get from operant
rdr[*readNum].id=cutToStr(string);// put id in struct
printf("please add the reader's first name:\n");
scanf("%s",string);// get from operant
rdr[*readNum].first_name=cutToStr(string);// put first name in struct
printf("please add the reader's last name:\n");
scanf("%s",string);// get from operant
rdr[*readNum].last_name=cutToStr(string);// put last name in struct
printf("please add the reader's address:\n");
scanf("%s",string);// get from operant
rdr[*readNum].address=cutToStr(string);// put adress in struct
printf("please add the reader's phone:\n");
scanf("%s",string);// get from operant
rdr[*readNum].phone=cutToStr(string);// put phone in struct
rdr[*readNum].numToTake = 5;// change value of numbers to tke to 5
*readNum = *readNum + 1;// rise the number of the readers
return rdr;// return the new structs array
}

在 main 中调用函数:

rdr=addReader(rdr,readNum);

阅读器结构的定义:

typedef struct reader{
int numToTake;
char *first_name, *last_name, *address, *phone, *id;
}reader;

我做错了什么?问候,大卫

编辑!

还是不行。新版代码:

void addReader(reader **rdr, int *readNum){     //adding a reader from the user
char string[1000];
rdr = (reader**)realloc(rdr,sizeof(reader*)*(*readNum+1));// build new struct array ( bigger), with the old values
checkreader(*rdr);// check if can get memory
printf("please enter the I.D. of the student you want to add:\n");
scanf("%s",string);// get from operant
rdr[*readNum]->id=cutToStr(string);// put id in struct
printf("please add the reader's first name:\n");
scanf("%s",string);// get from operant
rdr[*readNum]->first_name=cutToStr(string);// put first name in struct
printf("please add the reader's last name:\n");
scanf("%s",string);// get from operant
rdr[*readNum]->last_name=cutToStr(string);// put last name in struct
printf("please add the reader's address:\n");
scanf("%s",string);// get from operant
rdr[*readNum]->address=cutToStr(string);// put adress in struct
printf("please add the reader's phone:\n");
scanf("%s",string);// get from operant
rdr[*readNum]->phone=cutToStr(string);// put phone in struct
rdr[*readNum]->numToTake = 5;// change value of numbers to tke to 5
*readNum = *readNum + 1;// rise the number of the readers
//return rdr;// return the new structs array

main 中的新调用:

addReader(&rdr,readNum);

最佳答案

你应该传递 **rdr:

reader* addReader(reader **rdr, int *readNum)
^^

此外,这会在您realloc 时添加一个新的间接级别。

使用您现在拥有的代码,您可以在指针的副本而不是实际指针上重新分配

关于c - 退出函数后,使用 realloc 不会将值存储在数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8838504/

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