我想为链表函数创建静态或动态库,但是当我尝试使用 gcc
编译函数文件以获取目标文件时,它给了我错误 'struct node' 在内部声明参数表
int GetNth(struct node* head, int index)
& 解引用指向不完整类型“struct node”的指针 if(i == index) return head->data;
。不确定发生了什么,可能是因为我没有在文件中声明结构
#include <stdlib.h>
int count(struct node* head, int n) //counts the occurrence of n in the list
{
int c = 0;
while(head != NULL){
if(head->data == n) c+=1;
head = head->next;
}
return c;
}
但是如果我在这个文件中声明它,我认为它将违反 1 条定义规则。该怎么办?
结构在主函数中声明为
struct node{
int data;
struct node *next;
}
最佳答案
在使用对象之前(当有权访问其成员时使用它),您必须完全定义它们。因此,要使用struct node
,请在其首次出现在源代码中之前对其进行声明。这是C 编程语言的规则。
如果您想创建有用的库,请将声明放入头文件中并将该文件包含到源文件中,例如:
...
struct node
{
...
};
int count(struct node *head, int n);
...
关于c - 如何制作链表函数库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44385037/