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

标签 c pointers struct linked-list

我正在尝试用 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/

相关文章:

c - 同时生成真正的随机整数?

c - 它的输出是什么?为什么?

c - 尝试更新数组元素时出错

javascript - parse.com 使用 Javascript 多对多获取指针 ID 列表

c - 空结构上的段错误?

c - 函数调用中数据复制到函数参数不匹配

C 使用信号停止子进程

iphone - Objective-C指针/内存管理问题

c - 结构中一个字段的值无意中变成了另一个字段的值

c - 在结构中访问和设置数组值