Can the c language structure be used as a global variable? The global variable in c, the global structure uses

1) A function defined in A, how to call it in B?

If there is a header file, declare it in the header file and #include the header file in the B file.

If it is declared in the .c file, add extent declaration in B: extern void timer_hw_init(void);

2) How to call the structures and variables defined in A in B?

1> For the global structure definition, first construct the structure in the Globals.h header file, using the typedef method, such as:

typedef struct tagLineType

{

int ID;

CString Type;

double r0;

double x0;

}LINETYPE; // The name can be whatever you want, in the Globals.cpp file:

LINETYPE *lineType = NULL; // Note, initialized here

Then, go back to the Globals.h header file:

extern LINETYPE *lineType; // Note, this cannot be initialized again

2> For the definition of ordinary global variables (such as int type), first in Globals.cpp:

int iTime = -1; // note, initialized here

Then, in the Globals.h header file:

extern int iTime; // Note, can't be initialized again here

3> For global constant definitions, first in Globals.cpp:

const int UB = 10;

Then in the Globals.h header file:

extern const int UB

以上是第一种方法,是我自己总结试验成功的。用该方法定义完毕后,无论哪个.cpp文件要用到全局变量,只需在该.cpp文件中#include "Globals.h"即可,而无需再在.cpp文件中extern,所有全局变量、全局常量、全局结构体都是可见的。该方法的总体思路是,象theApp那样,在.cpp中定义,在.h头文件中extern声明。另外要注意,变量的初始化一定在Globals.cpp文件中,而不能在Globals.h头文件中。

结构体:每个用到这个全局结构体的文件都要包含typedef的头文件,声明该结构体要在.c文件中,而且不能包括在函数内。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324512226&siteId=291194637