2017-06-01 107 views
1

我在C中創建了一個聯繫人鏈接列表。它工作正常。但現在我想寫一個指定聯繫人的刪除功能(按名稱)我得到Error:"dereferencing pointer to incomplete type"。這裏是我的代碼:(儘管定義結構接觸)C:解除指向不完整類型錯誤的指針

struct contact 
{ 
    char name[100]; 
    char number[20]; 
    struct contact *next; 
}; 
int deleteByName(struct contact **hptr, char *name) 
{ 
    struct student *prev = NULL; 
    struct student *temp = *hptr; 
    while (strcmp(temp->name /*The Error is Here*/ , name) != 0 && (temp->next) != NULL) 
    { 
     prev = temp; 
     temp = temp->next; 
    } 
    if (strcmp(temp->name, name) == 0) 
    { 
     if (prev == NULL) 
      *hptr = temp->next; 
     else 
      prev->next = temp->next; 
     free(temp); 
     return 0; 
    } 
    printf("\nNAME '%s' WAS NOT FOUND TO BE DELETED.", name); 
    return -1; 
} 

我想知道爲什麼我得到這個錯誤。謝謝。

+8

可能因爲你定義了'struct contact',但'temp'被定義爲一個指向'struct student'的指針。 – yeputons

+0

你好,歡迎來到StackOverflow!可悲的是,你沒有提供足夠的代碼來幫助你正確地解決你的問題。請閱讀[問]並用[mcve]更新您的問題。 –

回答

0

next指針類型爲contact - 假設是一個錯字 - 這裏的更正後的代碼與錯別字固定 - 這compiles- HTH!

struct student 
{ 
    char name[100]; 
    char number[20]; 
    struct student *next; 
}; 

int deleteByName(struct student **hptr, char *name) 
{ 
    struct student *prev = NULL; 
    struct student *temp = *hptr; 
    while (strcmp(temp->name, name) != 0 && (temp->next) != NULL) 
    { 
     prev = temp; 
     temp = temp->next; //***No Error now*** 
    } 
    if (strcmp(temp->name, name) == 0) 
    { 
     if (prev == NULL) 
      *hptr = temp->next; 
     else 
      prev->next = temp->next; 
     free(temp); 
     return 0; 
    } 
    printf("\nNAME '%s' WAS NOT FOUND TO BE DELETED.", name); 
    return -1; 
} 
+0

對不起,這部分是好的,我在顯示錯誤行時出錯。我現在糾正了它。 –

+0

你已經定義了'struct student * temp'你能發佈你的'student'的聲明嗎? – Zakir

+0

「結構學生」本身就是一種類型,我將它定義在頂部。 –

相關問題