輸入文件:指針值改變
JOHN 9999999999
MEENA 9888888888
RIA 8718218218
SONIA 7777777777
FINCH 1234567890
我想通過附加文件的內容到list.I創建一個前哨以「沒有價值」進入它創建一個循環鏈表。
當我在gdb p-> next->名稱處測試我的代碼時,uploadAddresses()中的while循環結束時顯示了Finch.How是否可能?p值是否保持相同只有temp值在每個迭代。
#include<stdio.h> #include<stdlib.h> #include<string.h> #define MAXPHONENO 100 //Variables declaration struct Contact { char *name; char *mobileno; struct Contact *next; }; typedef struct Contact Contact; typedef Contact* PHBOOK; //Function declaration PHBOOK createAddressBook(); PHBOOK uploadAddresses(PHBOOK pb,char *name); //Implementation PHBOOK createAddressBook(){ PHBOOK pb = (PHBOOK)malloc(sizeof(Contact)); pb->name = "NO ENTRY"; pb->mobileno = "NO ENTRY"; pb->next = pb; return pb; } PHBOOK uploadAddresses(PHBOOK p,char *fname){ PHBOOK temp = p; char mob[20],name[20]; FILE *fp = fopen(fname,"r"); while(fscanf(fp,"%s %s",name,mob)!=EOF){ temp->next = (PHBOOK)malloc(sizeof(Contact)); temp = temp->next; temp->name = name; temp->mobileno = mob; printf("%s %s\n",name,mob); } temp->next = p; return p; } int main(){ PHBOOK pb = createAddressBook(); pb = uploadAddresses(pb,"/home/user/names_mobile"); return 0; }
函數'createAddressBook'的代碼在哪裏? –
我已經發布了createAddressBook()的代碼,現在 – user3310015
如果你想要一個循環鏈表,爲什麼你只指向下一個項目,而不是前一個?另外,爲了定義列表的最後一個元素,指向下一個元素的指針被用來定義爲'null',而不是它本身。 – adripanico