C错误解除引用指向不完整类型链表dic的指针

时间:2013-08-31 07:22:59

标签: c pointers struct linked-list

大家好,我看过其他类似问题的问题,但找不到类似的东西。我在代码的第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;
}

1 个答案:

答案 0 :(得分: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;