开发者

How to share structures within several files in my program?

Here's my problem: I have a big main.c file containing 40 or so "global" structures in my program (they are declared at the begining on the files), I also have several functions in my main.c file that I are able to directly read and write into these structures since they are global. Now I'm trying to move several func开发者_JAVA百科tion of my original main.c file into another .c file that would contain only the functions related to a specific part of my program. But of course I cannot directly access the main.c global variables from my new .c file. Is there a way around this? I'd like to avoid passing every structure by pointer as this would get horrible function prototypes. thanks


Move the global structure definitions into a header (.h) file and just #include your header in each c file that needs to access those structures. Any global variables can be declared extern and then defined in your main.c.

Global.h

// WARNING: SHARING DATA GLOBALLY IS BAD PRACTICE

#ifndef GLOBAL_H
#define GLOBAL_H

//Any type definitions needed
typedef struct a_struct
{
    int var1;
    long var2;
} a_struct;

//Global data that will be defined in main.c
extern a_struct GlobalStruct;
extern int GlobalCounter;

#endif

main.c

#include "Global.h"
#include "other.h"

#include <stdio.h>

int GlobalCounter;
a_struct GlobalStruct;

int main(int argc, char *argv[])
{
    GlobalCounter = 5;

    GlobalStruct.var1 = 10;
    GlobalStruct.var2 = 6;

    printf("Counter: %d\nStruct [ var1: %d var2: %ld ]\n", 
           GlobalCounter, GlobalStruct.var1, GlobalStruct.var2);

    do_other_stuff();

    printf("Counter: %d\nStruct [ var1: %d var2: %ld ]\n", 
           GlobalCounter, GlobalStruct.var1, GlobalStruct.var2);

    return 0;   
}

other.h

#ifndef OTHER_H
#define OTHER_H

void do_other_stuff(void);

#endif

other.c

#include "other.h"
#include "Global.h"

void do_other_stuff(void)
{
    GlobalCounter++;
    GlobalStruct.var1 = 100;
    GlobalStruct.var2 = 0xFFFFFF;
}


You can access them using extern.

In main.c:

MY_STRUCT my_global_struct;

In otherfile.c:

extern MY_STRUCT my_global_struct;


  1. Create a header file with the declarations for your structs
  2. include this header file where needed.

EDIT Sorry for this unfriendly answer.

As you mentioned in your comment, you usually declare your function prototypes in header files. You could use the header files also for declaring:

  • variables
  • structures

As structures are like classes in C++ in this case, you have to declare the complete body of the struct, to access the struct-members. If you use functions in the struct, you should to implement them in a separate C file.


I think global variables should usually be avoided, but to answer your question, you should declare all your structures in a .h file with the extern keyword:

// my_structs.h
struct A
{
   char c;
};

extern struct A myA; // declare the struct with extern, meaning it will be linked from a different compilation unit

// main.c

#include "my_structs.h"
struct A myA; // actually define struct, all others will be linked to this by the linker
//...

// other.c

#include "my_structs.h"

void accessMyA()
{
    printf ("%c", myA.c);
}


Ditto to Joe on the right answer. But I'd add that you should avoid using global data. This makes data flow between functions and modules difficult to track and mysterious. Better to pass the data you need explicitly in most cases.


If you are ok exposing the structures, then use the header approach from a few other answers. If not, then pass around void pointers in the global interface and cast to the structure type in the relevant source files.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜