Let's say I have the following construct
typedef struct Article {
char * word;
int code;
} Artcle;
And I want a bunch of files to have access to what amounts to:
Article THE = (Article) {.word="The", .code=87};
Article A = (Article) {.word="A", .code=79};
Article AN = (Article) {.word="An", .code=80};
What would be the proper way to put this into a shared article.c/h
file? My first thought was doing:
// article.h
typedef struct Article {
char * word;
int code;
} Artcle;
Article THE = (Article) {.word="The", .code=87};
Article A = (Article) {.word="A", .code=79};
Article AN = (Article) {.word="An", .code=80};
But this is obviously wrong with the multiple definitions for the Article
s. What would be the proper way to do this then?
One possible way to do this (I thought) was:
// article.h
typedef struct Article {
char * word;
int code;
} Artcle;
extern Article THE;
extern Article A;
extern Article AN;
// article.c
#include "article.h"
Article THE = (Article) {.word="The", .code=87};
Article A = (Article) {.word="A", .code=79};
Article AN = (Article) {.word="An", .code=80};
Additionally, what are these 'shared variables' referred to as?
question from:https://stackoverflow.com/questions/65946173/put-shared-definitions-in-an-external-file