2017-06-29 101 views
-2

我試圖在C中創建一個鏈接列表,其中包含帶有數據的struct;但是,我不能由於指針的一些問題。我在c中是全新的。這是我的代碼:主要目標是編寫的名字,並將它們存儲在一個LinkedList包含一個struct鏈接列表C中包含結構

(我得到這個錯誤錯誤:

incompatible types when assigning to type ‘Contacto {aka struct }’ from type ‘void *’ enlace->contact = malloc(sizeof(contact));)

#include <stdio.h> 
#include <stdlib.h> 

    typedef struct 
    { 
     char name[50]; 
     char apellido[50]; 

    } Contacto ; 

    typedef struct nodo { 
     Contacto contact; 
     struct nodo* siguiente; 
    } nodo; 

     nodo *cabecera = NULL; 
     nodo *actual = NULL; 

     void viewNames() 
     { 
      nodo *ptr; 
      ptr = cabecera; 

      printf("**********************************"); 
      while(ptr != NULL) {   
      printf("%s",ptr->contact.nombre); 
      ptr = ptr->siguiente; 
    } 

      printf("**********************************"); 
} 



    int main (int argc, char *argv[]) 
    { 



     nodo *enlace; 
     char nom; 
     int cont=0; 
     Contacto contact; 


     while (1){ 
       printf("Write names or 0 to exit "); 
       scanf("%s",&nom); 

       if (nom == '0') { 
        cabecera = enlace; 
        break; 
       } else { 
        enlace = (nodo *) malloc(sizeof(nodo)); 

        enlace->contact = malloc(sizeof(contact)); 
        strcpy(enlace->contact.nombre, nom); 
        enlace->siguiente = actual; 
        actual = enlace; 
     } 
    } 

    viewNames(); 
} 
+0

「由於指針的一些問題「。你遇到什麼具體問題?你不能只說「一些問題」,並期望人們知道你有什麼確切的問題或問題。它編譯失敗嗎?崩潰?產生錯誤的結果?究竟是什麼? – kaylum

+0

很抱歉,我得到這個錯誤錯誤:分配給輸入「CONTACTO {又名STRUCT }」從類型「void *的」當不兼容的類型 enlace->接觸= malloc的(的sizeof(接觸)); – jadev

+1

這裏的問題的負載:字符串的scanf到char變量中。與「contact.nombre」不匹配的代碼? malloc的非指針變量'enlace-> contact'就夠了。我放棄。 – John3136

回答

0

隨着錯別字很多,你已經在你的代碼的一些問題:

  1. 傳遞一個字符指針scanf("%s",&nom);我想你想讀一個字符串,而不是你傳遞一個字符的地址(因爲字符數組的指針內部;)) 。然後你應該聲明nom作爲一個字符數組並以此方式使用它。
  2. 你爲什麼試圖爲node->contact這裏分配內存:enlace->contact = malloc(sizeof(contact)); 你已經聲明它是一個普通的結構變量(而不是指針),所以你不需要爲它分配內存。

  3. viewNames()方法,也許你的意思是:printf("%s",ptr->contact.name);。由於查看您發佈的代碼,Contacto已經獲得了此字段,而不是您嘗試訪問的那個字段。

  4. 如果您想使用strcpy,將nom複製到動態分配的結構中,則必須包含C String library

我改變你的代碼位:

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> // 

typedef struct 
{ 
    char name[50]; 
    char apellido[50]; 
} Contacto ; 

typedef struct node { 
    Contacto contact; // 
    struct node* siguiente; 
} node; 

node *cabecera = NULL; 
node *actual = NULL; 

void viewNames() 
{ 
    node *ptr; 
    ptr = cabecera; 

    printf("**********************************"); 
    while(ptr != NULL) {   
    printf("%s",ptr->contact.name); // 
    ptr = ptr->siguiente; 
    } 

    printf("**********************************"); 
} 

int main (int argc, char *argv[]) 
{ 
    node *enlace; 
    char nom[100]; // 
    int cont=0; 
    Contacto contact; 

    while (1){ 
    printf("Write names or 0 to exit "); 
    scanf("%s",nom); 

    if (nom[0] == '0') { 
     cabecera = enlace; 
     break; 
    } else { 
     enlace = (node *) malloc(sizeof(node)); 

     //enlace->contact = (Contacto *)malloc(sizeof(Contacto)); // 
     strcpy(enlace->contact.name, nom); 
     enlace->siguiente = actual; 
     actual = enlace; 
    } 
    } 

    viewNames(); 
} 

當我運行它,我能看到你從用戶拿着鏈表元素:

~/work : $ g++ testLL2.cpp 
~/work : $ ./a.out 
Write names or 0 to exit Rohan 
Write names or 0 to exit Rohit 
Write names or 0 to exit Raman 
Write names or 0 to exit Ronny 
Write names or 0 to exit Reena 
Write names or 0 to exit 0 
**********************************ReenaRonnyRamanRohitRohan**********************************~/work : $ 
+0

@Downvoter:請說明你採取行動的理由。我會更新如果我失去了一些東西。 –

+0

Thankkks!那就是我想要的,我有點困惑於指針,我需要多練習 – jadev