Reputation: 231
I want to use insertion() from inside LinkedList.c. I am not able to pass the pointer into insertion.h file from another file.
error: head->data I am getting the below error: "pointer to incomplete class type is not allowed Node *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;
}
I am new to C, pls let me know if I am missing something.
Upvotes: 0
Views: 894
Reputation: 409166
The other file you use, Insertion.h
, should not be a header file. Instead it should be a source file.
You should still have a header file, but it should contain the structure instead. And both source files should include that header file.
Something like
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)
{
...
}
The you build and link the two source files, and both will know of the structure.
Also note that I have added null-pointer checks for the pointers before you dereference them. And changed the printf
format specifier used to print the pointer.
Upvotes: 3