If I had a header file named "Header1.h" and another header file named "Header2.h", and include the first header file into the second, Header1 -> Header2, I would have access to Header1's code from Header2 . (For example, if I had defined a struct inside Header1, I could now make variables of that struct type in Header2).
Now if I had a .c file named "Main.c" and if I include "Header2.h" in "Main.c", Header2 -> Main, I would be able to access Header2's code from Main, but I would also be able to access Header1's code from Main as if I have included "Header1.h" inside "Main.c" specifically as well, Header1 -> Main. (For example, I could make a variable of type struct [the one defined in "Header1.h"] inside "Main.c").
Is there a way for me to restrict such access to "Header1.h" from "Main.c" and allow only "Header2.h" to be able to access information from "Header2.h", and perform similar "privatization" of a header file?
Transcribing a comment into the question:
Header1.h
:
void printLinkedListImpl(Node* head)
{
while (head) {
printf("%d ", head->data);
head = head->next;
}
printf("\n");
}
Header2.h
:
typedef struct {
Node* head;
int size;
void (*printLinkedList)(Node*);
} LinkedList;
LinkedList* LinkedListConstructorImpl() {
LinkedList* linkedList = (LinkedList*)malloc(sizeof(LinkedList));
linkedList->head = NULL;
linkedList->size = 0;
linkedList->printLinkedList = &printLinkedListImpl;
}
Main.c
:
void runner() {
LinkedList* linkedList = LinkedListConstructorImpl();
}