배열의 데이터베이스에 값을 입력하고 함수 내에서 addReader
으로 값을 전송하면 값이 성공적으로 저장되지만 다시 main으로 돌아갈 때 입력 된 값은 사라집니다.함수를 종료 한 후 realloc을 사용하여 값을 배열에 저장하지 않습니다.
주어진 다음 코드
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 호출 함수 주어 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;
}
와 동적 배열을 만드는 독자 struct :
typedef struct reader{
int numToTake;
char *first_name, *last_name, *address, *phone, *id;
}reader;
내가 뭘 잘못하고 있니? 감사합니다, David
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);
'cutToStr'의 기능은 무엇입니까? 동일한 문자열에 대한 포인터를 반환하면이 문자열은 계속해서 다시 덮어 쓰게됩니다. 'readerBuilder'도 사용됩니까? 그리고'addReader'에 전달 된'rdr'은 어떻게 초기화됩니까? – ugoren
"cutstr"은 char의 정적 배열을 가져오고 동적 할당 char * (구조체의 셀에 사용됨)에 포인터를 반환합니다. –