我想在LinkedList.c中使用insert()。我无法将指针从另一个文件传递给insertion.h文件。
错误:head->数据 我收到以下错误: “不允许指向不完整类类型的指针 节点*头“
Insertion.h
#include <stdio.h>
extern void insertion(struct Node *head)
{
printf("\n value present at head: %d", head->next->data);
printf("\nvalue of head %d\n", head);
}
LinkedList.c
#include <stdio.h>
#include "inserttion.h"
struct Node
{
int data; //size of int is 4
struct Node *next;
};
int main()
{
struct Node *head = NULL;
struct Node *second = NULL; //size of second is 8
struct Node *third = NULL;
int option;
head = (struct Node *)malloc(sizeof(struct Node));
second = (struct Node *)malloc(sizeof(struct Node));
third = (struct Node *)malloc(sizeof(struct Node));
head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = NULL;
insertion(head);
return 0;
}
我是C的新手,请告诉我,如果我遗失了什么。
答案 0 :(得分:3)
您使用的其他文件Insertion.h
不应是头文件。相反,它应该是源文件。
您仍然拥有头文件,但它应该包含结构。两个源文件都应包含该头文件。
像
这样的东西Insertion.h
#pragma once
struct Node
{
int data; //size of int is 4
struct Node *next;
};
Insertion.c
#include <stdio.h>
#include "Insertion.h"
void insertion(struct Node *head)
{
if (head != NULL && head->next != NULL)
printf("\n value present at head->next: %d", head->next->data);
printf("\nvalue of head %p\n", (void *) head);
}
MAIN.C
#include <stdio.h>
#include "Insertion.h"
int main(void)
{
...
}
您构建并链接两个源文件,两者都知道结构。
另请注意,在您取消引用指针之前,我已为指针添加了空指针检查。并更改了用于打印指针的printf
格式说明符。