gpt4 book ai didi

c - C 中的链表包含结构体

转载 作者:行者123 更新时间:2023-11-30 20:33:35 25 4
gpt4 key购买 nike

我正在尝试用 C 语言创建一个链表,其中包含带有数据的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();
}

最佳答案

除了大量拼写错误之外,您的代码还存在一些问题:

  1. 将字符指针传递给 scanf("%s",&nom);。我想您想读取一个字符串,而不是传递一个字符的地址(因为字符数组内部是指针;))。然后您应该将 nom 声明为字符数组并以这种方式使用它。
  2. 为什么要在这里为 node->contact 分配内存:enlace->contact = malloc(sizeof(contact));您已将其声明为普通结构变量(而不是指针),因此无需为其分配内存。

  3. viewNames() 方法中,也许您的意思是:printf("%s",ptr->contact.name);。由于查看了您发布的代码,Contacto 已获得此字段,而不是您尝试访问的字段。

  4. 如果您想使用 strcpynom 复制到动态分配的结构中,则必须包含 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 : $

关于c - C 中的链表包含结构体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44816713/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com