I have 2 source files (.c) with the names file1.c and file2.c that need to split the variable between them, so if the variable is updated in one source file, then in the other source file, when you access this variable, the change will be visible .
what I did creates another source file called file3.c and a header file named file3.h (which, of course, is 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 that update is called in file1.c
and myvariable is updated, this change cannot be seen in file2.c
, because in file2.c, when get and myvariable are called, it prints, then 0 is printed only if update2.c is called in the file, then this change has been noticed. It looks like the variable is shared, but for each source file for that variable there is a different variable value / different memory.
user928774
source share