-2

我遇到了几个编译错误,但我无法弄清楚。这可能很简单,但我无法弄清楚。我猜对指针的引用必须改变,但我不知道到底是什么。我试图更改指针,但它仍然会出错。我将不胜感激。这些错误是:

  passing argument 1 of 'compare' from incompatible pointer type
  expected 'struct person *' but argument is of type 'char *'
  passing argument 2 of 'compare' from incompatible pointer type
  expected 'struct person *' but argument is of type 'char *'


  struct person *insert(struct person *head, char *personName, int personAge, int        (*compare)(struct person *a, struct person *b)) 
  {  
    struct person *new;

    new = (struct person*)malloc(sizeof(struct person));
    if(new == NULL)
    fprintf(stderr,"Couldn't allocate memory!");

    new->name = personName;
    new->age = personAge;  

    if(head == NULL)
    { 
      new->next = head;
      head = new; 
    }

    else
    {
      while(head != NULL)
      head = head->next;

      //compile errors
      if(compare(new->name,head->name) < 0)
      { 
        new->next=head;
     head->next=NULL;
      }

      else
      {
        head->next = new;
        new->next = NULL;
      }
    }//else  
      return head;

  }//method



  //----------------------------compare--------------------------------//
   int compare(struct person *a, struct person *b)
   {
      int result = strcmp(a->name, b->name);
      return result;
   }
4

3 回答 3

2

只是尝试compare(new->name,head->name)替代compare(new, head)

于 2012-09-10T12:04:23.413 回答
2

你不应该使用compare(new,head)代替compare(new->name,head->name)吗?

于 2012-09-10T12:04:42.860 回答
1

您的compare函数需要 2 个person指针,但您传递给它 2 个char指针(名称)。

于 2012-09-10T12:04:17.057 回答