2017-03-16 48 views
-1

我已經在C中編寫了自己的列表,但在向其中添加第二個對象時,我拋出了異常:寫入訪問衝突。它發生在addToList()函數中(我評論過該行)。你能告訴我爲什麼我得到一個錯誤。我一直在初始化主要方法頭:將結構添加到列表中時出現Nullptr錯誤

void main() 
{ 
    struct List *head = malloc(sizeof(head)); 
    head->next = NULL; 
    head->person = NULL; 
    //head = NULL; 
    addToList(head,"Abdul","Marin","21312321321"); 
    addToList(head, "Karmowski", "Byk", "24214541"); 
    displayList(head); 
    getchar(); 
    return 0; 

} 

List.h文件:

struct List 
    { 
     struct Person *person; 
     struct List *next; 
    }; 

List.c文件:

void addToList(struct List * head, char *name, char *surname, char *id) 
{ 
    struct List *tmp = head; 
    struct Osoba *newPerson= malloc(sizeof *newPerson); 
    newPerson->name = name; 
    newPerson->surname = surname; 
    newPerson->id = id; 
    if (tmp->person==NULL) 
    { 
     head->person = newPerson; 
    } 
    else if (tmp->next==NULL) 
    { 
     head->next->person = newPerson; // HERE OCCURES ERROR 
    } 
    else 
    { 
     while (tmp->next!=NULL) 
     { 
      tmp = tmp->next; 
     } 
     tmp->next->person = newPerson; 
    } 

} 
+1

您確定'tmp = head'和'tmp-> next == NULL',那麼您爲什麼期望能夠訪問'head-> next-> person'? – UnholySheep

回答

1
**/*function definition for add data in list */ 
struct list * AddList (struct list *head, char *name, char *surname, int id) 
{ 
     struct person *new_person = NULL ; 
     struct list *newlist = NULL ; 
     struct list *tmp = head ; 

     /* memory allocation for new person */ 
     new_person = (struct person *) malloc (sizeof (struct person)) ; 
     if (NULL == new_person) { 
       perror ("new node malloc failed : \n") ; 
       return NULL ; 
     } 

     /* Assign value */ 
     strcpy(new_person->name , name) ; 
     strcpy(new_person->surname , surname) ; 
     new_person->id = id ; 

     /* Assign data for head node */ 
     if (NULL == head->person ) { 
       head->person = new_person ; 
       return head ; 
     } else if (NULL == head->next) { /* 2nd Node */ 
       newlist = (struct list *) malloc (sizeof(struct list)); 
       newlist->person = new_person; 
       newlist->next = NULL ; 
       head->next = newlist ; 
       return head->next ; 
     } else { /* New list */ 
       while (tmp->next) { 
         tmp = tmp->next ; 
       } 

       /* memory allocation for new list */ 
       newlist = (struct list *) malloc (sizeof(struct list)); 
       newlist->person = new_person; 
       newlist->next = NULL ; 
       tmp->next = newlist ; 
       return head->next ; 
     } 
     return NULL ; 
} 

評論:

  • 您尚未爲新列表分配內存。
  • 分配的值在elseif和else塊中應該是錯誤的。
  • 在這裏,我附上了我的代碼的基本版本。我將在短時間內添加version2
0

這裏有一個問題

struct List *tmp = head; 
struct Osoba *newPerson= malloc(sizeof *newPerson); 
newPerson->name = name; 
newPerson->surname = surname; 
newPerson->id = id; 
if (tmp->person==NULL) 

您不能將名稱分配給newPerson->名稱,並希望它在該函數的作用域之外持續存在。

您需要的姓名分配空間,然後複製串有

例如

newPerson->name = strdup(name); // malloc/strcpy 
0
else if (tmp->next==NULL) 
{ 
    head->next = malloc(sizeof(struct List)); 
    head->next->person = newPerson; 
    head->next->next = NULL; 
} 

,並在最後else塊相同。

雖然實際上中間塊是不必要的;如果tmp->next==NULL那麼while循環將首次跳轉,所以你不需要把它作爲一個單獨的情況。

而且您最初的malloc看起來錯誤:

struct List *head = malloc(sizeof(head)); 

頭是一個指針。你一直在使用sizeof(* head),但我不喜歡它;更好的是(sizeof(struct Person))(它是類型,它規定了sizeof,而不是實例)。

+0

'sizeof struct Person' ==>'sizeof(struct Person)'。如果它是一個類型,那麼它們是必需的,如果它是一個變量,則是可選的。 – mch

+0

@mch oops是的,謝謝 –

+0

使用sizeof(* head)的確意味着如果頭的類型改變了,你不需要擔心更新對'malloc'的調用 –

0

您在訪問之前忘記了將內存分配給head -> next

添加

head->next = malloc(sizeof struct List);

就在

head->next->person = newPerson; // HERE OCCURES ERROR

相關問題