c - 如何制作链表函数库

标签 c struct static-libraries

我想为链表函数创建静态或动态库,但是当我尝试使用 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/

相关文章:

c - scanf() 函数不起作用?

ios - 创建 podspec 以发布静态库

ios - 框架和库的矛盾定义

c - 如何在节点结构中实现字符指针?

java - 通过 JNI 在 C 和 Java 之间传递指针

c++ - libSDL2main.a 的用途是什么?

c - 互斥锁已锁定,但其他线程正在进入临界区

c++ - POD 结构(相同类型的成员): are members in contiguous memory locations?

http - 如何将所有GET请求查询参数放入Go中的结构中?

c++ - 与 Boost 和 ncurses 的静态链接