0

I have a sub.h file that defines a variable and a structure, then in my sub.c I have a function that modify said variable and structure to a different value.

//main.c

#include <stdio.h>
#include <stdint.h>
#include "sub.h"

int main(void){
  
  change();
  printf("Main: a=%d - b=%d\n",trial->a,b);
  trial->a = 10;
  b = 15;
  printf("Main: a=%d - b=%d\n",trial->a,*ptr);
  
}
//sub.h

#include <stdint.h>
#include <stdio.h>

static struct TEST{
  int a;
}test;
static struct TEST *trial = &test;
static int b;
static int *ptr = &b;

void change(void);
//sub.c

#include "sub.h"

void change(void){
  trial->a = 20;
  *ptr = 25;
  printf("Change function: a=%d - b=%d\n",trial->a,b);
}
//Output
Change function: a=20 - b=25
Main: a=0 - b=0
Main: a=10 - b=15

When I call the function change() in my main the modified values would not be carried over. I'd like to know what I could do differently so that the values modified in sub.c would be carried to main.c

  • 2
    See [How do I use `extern` to share variables between source files?](https://stackoverflow.com/q/1433204/15168) —— You should not define variables in headers, especially if you plan to use GCC 10 or later. You declare variables in headers, but do not define them in headers. – Jonathan Leffler Jul 18 '23 at 19:09
  • In your code, you have independent sets of variables for `test`, `trial`, `b`, `ptr` in the two source files. The code in `main.c` cannot change the variables in `sub.c` and the code in `sub.c` cannot change the variables in `main.c` because they are defined as `static` variables in the header and hence are private to the separate source files. See the cross-referenced Q&A for how to share variables between files. – Jonathan Leffler Jul 18 '23 at 19:12
  • So apart from the initial "int external_variable = 1" declaration if i were to say "external_variable = 0" or "external_variable += 1" in another file would not be considered as a definition since I need a data type keyword for a definition? – th3virtuos0 Jul 18 '23 at 19:27
  • Redo: Your header should probably look more like: `struct TEST { int a; }; extern struct TEST test; extern struct TEST *trial; extern int b; extern int *ptr; extern void change(void);` —— Then you'd define the variables in one source file: `struct TEST test; struct TEST *trial = &test; int b; int *ptr = &b;` —— You'd include the header in both source files (as now), and include it before the definitions. I like the symmetry of `extern` before the function declaration — especially when there are `extern` variables too; many people loathe it. (_Previous edition was missing an `extern`!_) – Jonathan Leffler Jul 18 '23 at 19:55
  • If you're using global variables in production code, choose more significant names than `b` (in particular — the others are a bit imprecise too). In general, avoid using global variables. However, there are valid uses for them — consider the nuisance value if `stdout` and `stderr` were not global variables. – Jonathan Leffler Jul 18 '23 at 19:56

0 Answers0