Global structure in Header files

时间:2017-11-13 06:13:33

标签: c header structure global declaration

i have a question regarding structure in c and there declaration in header file. i am writing a code in which there are 3 variables which have another two variables linked with it. so i decided to declare a structure. below i'm writing a Pseudo-code

//----------registers.h---------
#ifndef __REG__H__
#define __REG__H__

struct ADE_reg_type
{
    unsigned int i : 4;
    unsigned int j : 4;
};

extern struct ADE_reg_type AIRMS_reg;
extern struct ADE_reg_type BIRMS_reg;
extern struct ADE_reg_type CIRMS_reg;
#endif


//-----------registers.c-------------------
#include "registers.h"

struct ADE_reg_type AIRMS_reg = {.i = 4,.j = 5};  //initializing 
struct ADE_reg_type BIRMS_reg = {.i = 3,.j = 4};  //initializing 
struct ADE_reg_type CIRMS_reg = {.i = 1,.j = 9};  //initializing 

//-----------main.c-------------------
#include "registers.h"

int main()
{
   display_numbers(AIRMS_reg);  //display_numbers(struct ADE_reg_type) has 
                              //defined in another .c file which also includes
                              // registers.h
}

What i'm want is that i should have a variable which i can access in any .c file. scope of variable should be global. so is it a right way to use a global structure or i'm making some mistake here ?

3 个答案:

答案 0 :(得分:2)

//----------registers.h---------
#ifndef __REG__H__
#define __REG__H__

struct ADE_reg_type
{
    unsigned int i : 4;
    unsigned int j : 4;
};


#endif


//-----------registers.c-------------------
#include "registers.h"

const struct ADE_reg_type AIRMS_reg = {.i = 4,.j = 5};  //initializing 
const struct ADE_reg_type BIRMS_reg = {.i = 3,.j = 4};  //initializing 
const struct ADE_reg_type CIRMS_reg = {.i = 1,.j = 9};  //initializing 

//-----------main.c-------------------
#include "registers.h"

extern const struct ADE_reg_type AIRMS_reg;

extern const struct ADE_reg_type BIRMS_reg;

extern const struct ADE_reg_type CIRMS_reg;

int main()
{
   display_numbers(AIRMS_reg);                               
}

//-----------------file_1.c--------------------
#include "registers.h"

extern const struct ADE_reg_type AIRMS_reg;

extern const struct ADE_reg_type BIRMS_reg;

extern const struct ADE_reg_type CIRMS_reg;

//-----------------file_2.c--------------------
#include "registers.h"

extern const struct ADE_reg_type AIRMS_reg;

extern const struct ADE_reg_type BIRMS_reg;

extern const struct ADE_reg_type CIRMS_reg;

就像你说的那样@Elad Hazan

答案 1 :(得分:1)

在某处定义一个全局变量(每种类型的课程),其余的将是extern ... 结构必须在头文件中,但永远不要在头文件中初始化或定义变量 - 这是一个坏习惯。

例如:

//----------registers.h---------
#ifndef __REG__H__
#define __REG__H__

struct ADE_reg_type
{
    unsigned int i : 4;
    unsigned int j : 4;
};


#endif


//-----------registers.c-------------------
#include "registers.h"

struct ADE_reg_type AIRMS_reg = {.i = 4,.j = 5};  //initializing 
struct ADE_reg_type BIRMS_reg = {.i = 3,.j = 4};  //initializing 
struct ADE_reg_type CIRMS_reg = {.i = 1,.j = 9};  //initializing 

//-----------main.c-------------------
#include "registers.h"

extern struct ADE_reg_type AIRMS_reg;

extern struct ADE_reg_type BIRMS_reg;

extern struct ADE_reg_type CIRMS_reg;

int main()
{
   display_numbers(AIRMS_reg);                               
}

答案 2 :(得分:0)

数组方法: 例如:

ADE_reg_type RegType_Array[3];
ADE_reg_type* ADE_reg_type(char index)
{
    return &RegType_Array[index];
}

变量方法:

ADE_reg_type AIRMS_reg = {.i = 4,.j = 5};
ADE_reg_type* AIRMS_reg()
{
    return &AIRMS_reg
}