I split a C program into multiple files. Here is what they look like.
ListDefinition.h
:
#ifndef ALGORITHM_AND_DATASTRUCTURE_LISTDEFINITION_H
#define ALGORITHM_AND_DATASTRUCTURE_LISTDEFINITION_H
extern typedef struct type DataType; //this is a struct declaration about basic data type in the list
extern typedef struct s_list SeqList, *PseqList; //this is a struct declaration about sequence list
extern typedef struct sl_list SlinkList, *PSlinkList; //this is a struct declaration about linked list
#endif //ALGORITHM_AND_DATASTRUCTURE_LISTDEFINITION_H
or I remove extern
#ifndef ALGORITHM_AND_DATASTRUCTURE_LISTDEFINITION_H
#define ALGORITHM_AND_DATASTRUCTURE_LISTDEFINITION_H
typedef struct type DataType; //this is a struct declaration about basic data type in the list
typedef struct s_list SeqList, *PseqList; //this is a struct declaration about sequence list
typedef struct sl_list SlinkList, *PSlinkList; //this is a struct declaration about linked list
#endif //ALGORITHM_AND_DATASTRUCTURE_LISTDEFINITION_H
ListDefinition.c
:
#include "ListDefinition.h"
#define MAXSIZE 100
typedef struct type {
int date;
}DataType;
typedef struct s_list {
DataType a[MAXSIZE];
int length;
int top;
}SeqList, *PseqList;
typedef struct sl_list {
DataType node;
struct sl_list *next;
}SlinkList, *PSlinkList;
I want to use ListDefinition.h
in ListFunction.h
ListFunction.h
:
#ifndef ALGORITHM_AND_DATASTRUCTURE_LISTFUNCTION_H
#define ALGORITHM_AND_DATASTRUCTURE_LISTFUNCTION_H
#include "ListDefinition.h"
PseqList initial_seqList(PseqList PL);//The function be used to initialize the sequence list
int search_seqlist(PseqList PL, DataType x);//the function be used to search the x in the sequence list
#endif //ALGORITHM_AND_DATASTRUCTURE_LISTFUNCTION_H
ListFunction.c
:
#include <stdio.h>
#include <stdlib.h>
#include "ListFunction.h"
PseqList initial_seqList(PseqList PL) {
PL=malloc(sizeof(SeqList));
if(PL == NULL) {
exit(1);
printf("The memory isn't allocated");
}
PL->length = 0;
PL->top = -1;
return PL;
}
int search_seqlist(PseqList PL, DataType x) {
int i;
for(i = 0;i < PL->length; i++) {
if(PL->a[i].date == x.date)
break;
}
if (i == PL->length)
return 0;
else
return i+1;
}
You don't care about what the codes mean. Many errors appear, but when I change #include "ListDefinition.h"
into #include "ListDefinition.c"
in ListFunction.h
. All errors go away, I want to know why? This problem seems to tell me I should use ListFunction.h
. I run the codes in Clion.