【问题标题】:c- Error Conlficting types within linked list structc-链表结构中的错误冲突类型
【发布时间】:2019-10-14 19:41:12
【问题描述】:

“我正在尝试实现一个链接列表,该链接列表稍后将使用字典排序功能,但我不断收到有关 listnode 中冲突类型的错误。

我不知道这个问题的原因是什么。以前我在尝试使用 sizeof 进行 malloc 时遇到问题,我发现解决方案将 next 声明为 listnode* 指针。它解决了这个问题,但它仍然有冲突的类型。当我尝试编译时,我收到以下错误消息:

ls.c:18:3: 错误:“listnode”的类型冲突
} 列表节点:
ls.c:12:14:请注意先前声明的“listnode”在这里
typedef 节点列表节点;

我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
int strcmp(const char *str1, const char *str2);

//This chunk of code is dedicated to the formation and functions of Linked list
//which will be used to store and sort file names.
typedef struct node node;
typedef node listnode;
#define EMPTY NULL;

typedef struct listnode{
  struct listnode* next;
  char* string;
} listnode;

struct listnode* head;

//This function will print the entire list
void printlist(listnode* head){
 struct listnode* current = head;

 while(current != NULL){
    printf("%s \n", current->string);
    current = current->next;
 }
}

//This function creates a new node
listnode* newNode(char* str, listnode* node){
  listnode* new = (listnode*)malloc(sizeof(listnode*));
  if(new == NULL){
    printf("Error creating new listnode.\n");
    exit(0);
  }
  new->string = str;
  new->next = node;

  return new;
}

//This function will append a new node onto the list
listnode* list_append(listnode* head, char* str){
  if (head == NULL){
    listnode* new = newNode(str, head);
    head = new;
    return head;
  }
  else{
    listnode* current = head;
    while(current-> next != NULL){
      current = current->next;
    }

    listnode* new = newNode(str,NULL);
    current -> next = new;
  }
    return head;
}


//This function earses the list freeing up space
void list_free(listnode* head){
  listnode* current;
  listnode* temp;

  if(head != NULL){
    current = head->next;

    if(head !=NULL){
      current = head -> next;
      head ->next = NULL;
      while(current != NULL){
    temp = current -> next;
    free(current);
    current = temp;
      }
    }
  }
  free(head);
}



//This is the end of the linked list code

int main(int argc, char **argv){
  char *current_dir = NULL;
  DIR *direct_ptr = NULL;
  struct dirent *dir_ptr = NULL;
  unsigned int fileNum = 0;
  int c;
  listnode* head = NULL;

  current_dir = getenv("PWD");
  if(NULL == current_dir){
    printf("\n Error: Couldn't grab current directory.\n");
    return -1;
  }

  direct_ptr = opendir((const char*)current_dir);
  if(NULL == direct_ptr){
    printf("\n Error: couldn't open current directory\n");
    return -1;
  }

  if(argc == 1){
    for(fileNum=0; NULL != (dir_ptr = readdir(direct_ptr)); fileNum++){
      if(dir_ptr->d_name[0] != '.'){
        head = list_append(head, dir_ptr->d_name);
      }
    }
  }
  else{
    if(strcmp(argv[1], "-a") || strcmp(argv[1], "[-a]")){
      for(fileNum=0; NULL != (dir_ptr = readdir(direct_ptr)); fileNum++){
        head = list_append(head, dir_ptr->d_name);
      }
    }
    else{
      printf("\n Unrecognized argument in command line.\n");
      return -1;
    }
  }
return 0;
}

【问题讨论】:

  • 你有typedef node listnode,然后几行之后你有另一个typedef,它定义了listnode

标签: c


【解决方案1】:

事实上,您对listnode 的定义存在冲突。你先在这里定义类型:

typedef struct node node;
typedef node listnode;

然后在这里再次定义:

typedef struct listnode{
  struct listnode* next;
  char* string;
} listnode;

在一种情况下,它是struct node 的别名,而在另一种情况下,它是struct listnode 的别名。

摆脱第一个以及typedef struct node node;

【讨论】:

    【解决方案2】:

    listnode 有两个定义:

    typedef node listnode;
    

    typedef struct listnode{
      struct listnode* next;
      char* string;
    } listnode;
    

    第一个是不必要的,不会起作用,因为它使用node,它被定义为

    typedef struct node node;
    

    但您从未在任何地方定义过struct node

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-06
      • 1970-01-01
      • 2014-03-10
      • 2013-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多