【问题标题】:C error dereferencing pointer to incomplete type linked list dicC 错误取消引用指向不完整类型链表 dic 的指针
【发布时间】:2013-08-31 07:22:59
【问题描述】:

大家好,我已经查看了其他类似问题的问题,但找不到类似的内容。我在代码的第 69 行遇到错误(根据标题),我不确定如何修复它。我的代码无法编译 atm。该程序旨在获取键值对并形成排序的字典链表。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
  int key;
  char value[256];
  struct node *next;
} node;

void findloc(int key, node*headnode);
void insert(node*newinserte, node*previous, node*after);

int main()
{
  makedic();
}

int makedic()
{
  int keydata, once;
  int compareval = 0;
  int i = 0;
  char valuedata[256];
  node * root, *head, *tmp;
  head = NULL;
  while (scanf("%d %s", &keydata, &valuedata) != EOF)
  {
    root = (node*) malloc(sizeof(node));
    root->key = keydata;
    strcpy(root->value, value data);
    root->next = head;
    head = root;
    if (head != NULL && once == 0)
    {
      tmp = head;
      once++;
    }
    findloc(root->key, tmp);
  }
  /*for(; p1->next!=NULL;p1 = p1->next)
   {
   for (p2 = p1->next;p2!=NULL;p2=p2->next)
   {
   if(p1->key>p2->key)
   {
   int temp = p1 ->key;
   p1 ->key = p2->key;
   p2 ->key =temp;
   compareval = compareval +1;
   }
   }
   }*/
  //root = root -> next;  
  while (root)
  {
    printf("%d %s\n", root->key, root->value);
    root = root->next;
  }
  printf("%d\n", compareval);
}

void findloc(int keysearch, node*headnode)
{
  int i;
  node*head, *root;
  head = headnode;
  while (headnode->next != NULL )
  {
/*line 69*/    if (keysearch < headnode->next->key) //error is here
    {
      if (keysearch > headnode->key)
      {
        //insert(headnode ,headnode->next,headnode->next->next );
      }
    }
  }
}

void insert(node*newinserte, node*previous, node*after)
{
  int tmp = after;
  previous->next = newinserte;
  newinserte->next = tmp;
}

【问题讨论】:

  • 您正在声明一个结构,该结构只能用于在声明中形成对象,而不是在其他任何地方,因为没有给结构命名。
  • 顺便说一句:int main() 应该返回一个int。并且不要像在 C 中那样强制转换 malloc/calloc/realloc,这既没有必要也不推荐:stackoverflow.com/a/605858/694576

标签: c pointers struct linked-list


【解决方案1】:

这个

typedef struct
{
  int key;
  char value[256];
  struct node *next;
} node;

应该是这样的:

typedef struct node
{
  int key;
  char value[256];
  struct node * next;
} node;

否则成员struct node * next 将指向未知类型,即struct node


注意:虽然前者是完全有效的,但为了减少可能的混淆,我会以以下方式声明它:

typedef struct node
{
  int key;
  char value[256];
  struct node * next;
} Node;

【讨论】:

  • 您的回答中的错字:会员应该是struct node * next
猜你喜欢
  • 2018-04-09
  • 2018-05-31
  • 2017-11-02
  • 1970-01-01
  • 1970-01-01
  • 2011-04-06
  • 1970-01-01
相关资源
最近更新 更多