When I try to compile this program without the struct in the functions.h
and functions.c
file, it works. But when using a struct, it doesn't work.
How do I properly use structs with these .h
and .c
files?
main.c file
#include <stdlib.h>
#include <stdio.h>
#include "functions.h"
int main(void) {
func1();
func2();
//make a linked list of persons
person * head = NULL;
head = (person *) malloc(sizeof(person));
if (head == NULL) {
return 1;
}
head->val = 1;
head->next = NULL;
return 0;
}
functions.h file
struct node;
typedef struct node person;
void func1(void);
void func2(void);
functions.c file
#include "functions.h"
struct node {
char name;
int age;
node *next;
};
void func1(void) {
printf("Function 1!\n");
}
void func2(void) {
printf("Function 2!\n");
}
Compile it with:
gcc -o main.exe main.c functions.c
functions.c
tomalloc(sizeof(struct node))
because only it knows the size.