将数据库中的值放入数组并将值发送到函数本身的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;
我做错了什么? 的问候,大卫
EDIT!
仍然无效。 新版代码:
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
}
主要的新呼叫:
addReader(&rdr,readNum);
答案 0 :(得分:2)
你应该通过**rdr
:
reader* addReader(reader **rdr, int *readNum)
^^
此外,这会在您realloc
时添加新的间接级别。
使用代码,您现在可以在指针的副本上realloc
而不是实际的指针。