i have 2 source files (.c) named file1.c and file2.c which need to share between them a variable, so that if in one source file the variable is been updated then in the other sourced file when accessing this variable the change will be seen.
what i did is create another source file called file3.c and header file called file3.h (which, of course, is been included in file1.c file2.c and in file3.c)
in file3.c:
int myvariable = 0;
void update(){//updating the variable
myvariable++;
}
int get(){//getting the variable
return myvariable;
}
in file3.h:
extern int myvariable;
void update(void);
int get(void);
in file1.c:
.
.
.
printf("myvariable = %d",get());//print 0
update();
printf("myvariable = %d",get());//print 1
.
.
.
in file2.c:
.
.
.
printf("myvariable = %d",get());//print 0 but should print 1
.
.
.
but the problem is when in file1.c
update is invoked and myvariable is updated
the change cannot be seen in file2.c
because in file2.c when get is invoked and
myvariable is printed then 0 is been printed, only if in file2.c update is invoked then the change is been seen.
it seems like the variable is shared but for each source file there is a different variable value/different memory for this variable