목록을 사용하고 목록의 두 번째 차원에 동적으로 메모리를 할당하고 해당 구조체를 채우는 특정 숙제 기능을 사용하여 다음 구조체를 사용하여 다소 어려움을 겪고 있습니다.2d 연결된 목록 메모리가 옮겨지지 않음
13 typedef struct BookIds{
14 int id;
15 struct BookIds* next;
16 }bookIds;
18 typedef struct Student{
//Unimportant struct elements
23 bookIds* wishlist;
24 struct Student* next;
25 }student;
하고
void addWishlist(student* head, char* wishListfile) {
//cannot modify parameters or return type
191 FILE* f = fopen(wishListfile, "r");
192 student* current = head;
193 int i;
194 int* wish = malloc(sizeof(int) * 4);
195 while (current != NULL) {
196 current->wishlist = malloc(sizeof(bookIds));
197 fscanf(f, "%d %d %d %d", (wish), (wish+1), (wish+2), (wish+3));
198 for (i = 0; i < 4; i++) {
199 current->wishlist->id = *(wish+i);
200 current->wishlist->next = malloc(sizeof(bookIds))
201 current->wishlist = current->wishlist->next;
202 }
203 current = current->next;
204 }
205 free(wish);
206 fclose(f);
207 }
문제를 호출하는 함수를 호출 한 후, 제 2 어레이리스트 메모리 (모든 소자가 NULL이다)이 발에 의해 패스 때문에 손실이다. 일반적으로 목록을 반환하거나 이중 포인터를 매개 변수로 사용하도록 선택 하겠지만이 할당에 대한 옵션은 아닙니다. 모든 도움/프로그래밍 조언을 많이 주시면 감사하겠습니다.
'new_node = malloc (sizeof (* new_node))와 같은 링크를 만듭니다;/* new_node의 멤버를 설정합니다 */new_node-> next = list_holder; list_holder = new_node;'또한'list_holder'는 NULL로 시작합니다. – BLUEPIXY