I'm learning how to create header files and separate the implementation while also starting to learn about how to create a linked list. I created linked_list.h
, linked_list.c
, and main.c
files. In main.c
I get an error when trying to create the struct I typedef'ed unless I create it as a pointer, then it works. I don't understand why this is, could someone please explain this?
linked_list.h
:
#ifndef LINKED_LIST_H_
#define LINKED_LIST_H_
typedef struct ListNode_ ListNode;
#endif // LINKED_LIST_H_
linked_list.c
:
#include <stdio.h>
#include <stdlib.h>
#include "linked_list.h"
struct ListNode_ {
int data;
ListNode *next;
};
main.c
:
#include <stdio.h>
#include <stdlib.h>
#include "linked_list.h"
int main() {
ListNode node1; // why can't I do this?
ListNode *node2; // but this works
}