C、 从另一个文件调用链表函数
我有两个文件,list_funcs.c和list_mgr.c。List_funcs.c具有将节点插入链接列表的功能:C、 从另一个文件调用链表函数,c,linked-list,C,Linked List,我有两个文件,list_funcs.c和list_mgr.c。List_funcs.c具有将节点插入链接列表的功能: #include <stdio.h> #include <string.h> #include <stdlib.h> struct data_node { char name [25]; int data; struct data_node *next; }; struct data_node * insert (struct data_n
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct data_node {
char name [25];
int data;
struct data_node *next;
};
struct data_node * insert (struct data_node **p_first, int elem) {
struct data_node *new_node, *prev, *current;
current=*p_first;
while (current != NULL && elem > current->data) {
prev=current;
current=current->next;
} /* end while */
/* current now points to position *before* which we need to insert */
new_node = (struct data_node *) malloc(sizeof(struct data_node));
new_node->data=elem;
new_node->next=current;
if ( current == *p_first ) /* insert before 1st element */
*p_first=new_node;
else /* now insert before current */
prev->next=new_node;
/* end if current == *p_first */
return new_node;
};
函数有三个参数,您只传递前两个
struct data_node * insert (struct data_node **, int, char *);
需要将指针传递给数据\u节点*
,然后是int
,最后是char*
类型
令人困惑的是,您对函数的定义也与声明不匹配,定义中省略了最后一个
char*
。您对insert
的定义如下:
struct data_node * insert (struct data_node **p_first, int elem)
struct data_node * insert (struct data_node **, int, char *);
但标题中的声明如下所示:
struct data_node * insert (struct data_node **p_first, int elem)
struct data_node * insert (struct data_node **, int, char *);
注意末尾的
char*
。您可能想删除它以使其匹配。您在列表\u func.h
中的函数原型有一个额外的参数:
struct data_node * insert (struct data_node **, int, char *);
/* one of these doesn't belong: ^ ^ */
因此,
list\u mgr.c
中的函数定义与list\u funcs.c
中的调用匹配,list\u func.h
中的原型不匹配。嗯,有点像。转发声明与实现不匹配,这才是真正的问题。您需要首先在main()
中初始化,但这不是编译问题。在list\u funcs.c文件中插入inlcude“list\u funcs.h”
,编译器将解释代码的错误。