Structure as global variable

I need to use the below global structure defined in code1.c in another code2.c

struct memIOptrs
{
const char *name;
unsigned char *virtptr;
}MEM_IO_PTRS[20];

I have a header file for the project codes.h, how should the structure be declared here.

Also, what if the structure was typedef like,
typedef struct memIOptrs
{
const char *name;
unsigned char *virtptr;
}MEMIOPTRS;
MEMIOPTRS MEM_IO_PTRS[20];

You'd define the type of the struct in the header file, and declare (but not define) MEM_IO_PTRS in the header using extern:

/* in header file */

struct mem_ptrs
{
    ... /* members */
};

extern struct mem_ptrs MEM_IO_PTRS[10];

And then in one of your C files you'd define MEM_IO_PTRS:

/* in source file */

struct mem_ptrs MEM_IO_PTRS[10];
1 Like