將數據庫中的值放入數組中並將值發送到函數本身內的addReader
後,值將成功存儲,但返回主輸入的值不見了。在退出函數後,使用realloc將不會存儲數組中的值
鑑於以下代碼:創建與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
}
鑑於主調用用於功能:
rdr=addReader(rdr,readNum);
Defenition讀者結構:
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
}
在主要的新電話:
addReader(&rdr,readNum);
'cutToStr'是做什麼用的?如果它返回一個指向同一個字符串的指針,那麼這個字符串將被一次又一次地覆蓋。並且'readerBuilder'甚至被使用?以及如何將'rdr'傳遞給'addReader'進行初始化? – ugoren 2012-01-12 16:31:12
「cutstr」獲取chars的靜態數組並返回指向動態分配char *的指針(用於結構的單元格)。 – 2012-01-12 16:43:20