C语言 在头文件中声明结构体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14699538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Declare struct in header file
提问by Nelson.b.austin
I want to declare a structure in a header file so I can use it in my source file. What am I doing wrong? I want to be able to access my struct from any function.
我想在头文件中声明一个结构,以便我可以在我的源文件中使用它。我究竟做错了什么?我希望能够从任何函数访问我的结构。
info.h
信息.h
#ifndef INFO_H
#define INFO_H
typedef struct info
{
int mem_size;
int start_loc;
int used_space;
int free_space;
} INFO;
#endif
test.c
测试.c
#include <stdio.h>
#include <stdlib.h>
#include <info.h>
#define F_first 1
#define F_last 2
#define F_data_int 3
#define F_data_char 4
#define F_data_float 5
#define F_print 6
void * f(int code);
int main() {
INFO in;
in.mem_size = 8;
f(F_last, 0,0);
return(0);
}
void * f(int code) {
printf("%d", in.mem_size);
}
采纳答案by Alok Save
Replace:
代替:
#include <info.h>
with,
和,
#include "info.h"
With <>compiler only searches for the header file in predesignated header folder. This is used for standard library header files.
With ""compiler first searches the header file in the local directory where your .c file is located. This is used for user defined header files.
使用<>编译器仅在预先指定的头文件夹中搜索头文件。这用于标准库头文件。
使用""编译器首先在 .c 文件所在的本地目录中搜索头文件。这用于用户定义的头文件。
回答by Rico
Yes... You must use #include "info.h"as opposed to #include <info.h">for custom based headers. Usually they are not part of the system branch which are normally located in the /usr/includedirectory on Unix/Linux platforms.
是的...您必须使用#include "info.h"而不是#include <info.h">用于基于自定义的标头。通常它们不是系统分支的一部分,系统分支通常位于/usr/includeUnix/Linux 平台上的目录中。

