【发布时间】:2020-02-11 15:54:38
【问题描述】:
当我尝试在没有functions.h 和functions.c 文件中的结构的情况下编译这个程序时,它可以工作。但是当使用结构时,它不起作用。
如何正确使用这些 .h 和 .c 文件的结构?
main.c 文件
#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 文件
struct node;
typedef struct node person;
void func1(void);
void func2(void);
functions.c 文件
#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");
}
编译:
gcc -o main.exe main.c functions.c
【问题讨论】:
-
直接在头部定义结构体。
-
你需要
functions.c到malloc(sizeof(struct node))因为只有它知道大小。
标签: c gcc struct compilation header