-2

Let suppose I have a one structure

 struct A
 {
 int x;
 int y;
 }s;

Now suppose I want to share this structure variable i.e. 's' in multiple c files. How can I achieve this thing.

Should I use extern or I have to use the header file and include that header file in each c file

vivek
  • 467
  • 2
  • 6
  • 18

1 Answers1

3

Split into header and code file.
Then include the header in each code file which needs to access the variable.
(Global variables should be used with care by the way.)

Header:

 struct A
 {
 int x;
 int y;
 };

extern A s;

A single code file:

#include "theheader.h"
A s;

Other code files:

#include "theheader.h"
/* access the variable */    
Yunnosch
  • 26,130
  • 9
  • 42
  • 54