vinnylinux
vinnylinux

Reputation: 7034

Creating a global struct for multiple source files

I have a common header file for multiple source files in a C project. I want a certain struct instance:

typedef struct
{
    char username[255];
    char password[255];
} Configuration;

Configuration config;

To be available for all source files, and any changes made to it in any of the source files, should affect all others. How is this possible?

Upvotes: 0

Views: 1206

Answers (1)

nos
nos

Reputation: 229342

In your header file you declare the config variable as extern:

extern Configuration config;

And in one, and only one, source file you define that variable like so:

Configuration config;

Upvotes: 5

Related Questions