C语言 不允许指向不完整类类型的指针

apeeds0o  于 2023-08-03  发布在  其他
关注(0)|答案(2)|浏览(263)

我正在实现一个冒泡排序函数,它对单词进行排序。交换功能的话完全好,但我无法得到的错误。试着在网上搜索,但找不到有用的东西。我在哪里得到了错误的标记。
谢谢你的帮助

  1. void sortWord (struct node** head) {
  2. struct node* temp = (*head);
  3. struct node* temp2 = (*head);
  4. int i;
  5. int j;
  6. int counter = 0;
  7. while(temp != NULL)
  8. {
  9. temp = temp->next; //<-- this is where i get the error.
  10. counter++;
  11. }
  12. for( i = 1; i<counter; i++)
  13. {
  14. temp2=(*head);
  15. for(j = 1; j<counter-1;j++)
  16. {
  17. if(wordCompare(temp2,nodeGetNextNode(temp2))>0)
  18. {
  19. swap(head,temp2,nodeGetNextNode(temp2));
  20. continue;
  21. }
  22. }
  23. temp2 = nodeGetNextNode(temp2);
  24. }
  25. }

字符串

xuo3flqw

xuo3flqw1#

当您尝试使用已前向声明但未 * 定义 * 的struct时,会出现此错误。虽然声明和操作指向此类结构体的 * 指针 * 是完全可以的,但尝试解引用它们是不可以的,因为编译器需要知道它们的大小和布局以便执行访问。
具体来说,在您的例子中,编译器不知道struct nodenext,所以

  1. temp->next

字符串
不编译。
您需要在定义sortWord函数的编译单元中包含struct node的定义来解决此问题。

raogr8fs

raogr8fs2#

您应该替换此行:

  1. temp = temp->next;

字符串
用这句话:

  1. temp = nodeGetNextNode(temp);


原因是在这段代码中,您对node的结构一无所知。我想这就是为什么你对temp2使用nodeGetNextNode函数的原因。你只需要用它来做临时工。

相关问题