There are two ways to accomplish this:
1.
Create a header file that contains the following statement:
/* header.h */
extern struct example obj;
Add the following definition to one and only one source file:
/* source.c */
struct example obj;
Any source file that needs direct access to obj
should include this header file.
/* otherFile.c */
#include "header.h"
void fun(void)
{
obj.a = 12;
obj.b = 13;
}
2.
Create getter/setter functions:
/* source.c */
static struct example obj;
void set(const struct example * const pExample)
{
if(pExample)
memcpy(&obj, pExample, sizeof(obj));
}
void get(struct example * const pExample)
{
if(pExample)
memcpy(pExample, &obj, sizeof(obj));
}
/* otherFile.c */
void fun(void)
{
struct example temp = {.a = 12, .b = 13};
set(&temp);
get(&temp);
}
With this method, obj
can be defined as static
.