我試圖在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();
}
「由於指針的一些問題「。你遇到什麼具體問題?你不能只說「一些問題」,並期望人們知道你有什麼確切的問題或問題。它編譯失敗嗎?崩潰?產生錯誤的結果?究竟是什麼? – kaylum
很抱歉,我得到這個錯誤錯誤:分配給輸入「CONTACTO {又名STRUCT}」從類型「void *的」當不兼容的類型 enlace->接觸= malloc的(的sizeof(接觸)); –
jadev
這裏的問題的負載:字符串的scanf到char變量中。與「contact.nombre」不匹配的代碼? malloc的非指針變量'enlace-> contact'就夠了。我放棄。 – John3136