My goal is to make a static variable 'val' available to a different .c file (just for experimentation) .
so i made a global pointer which holds the address of the static variable, and by this global pointer, i am trying to access the static variable's value in another file .
But,
static int val=33;
int *ptr;
ptr = &val;
gives me error.
while if i do it like this, it works .
static int val=33;
int *ptr = &val;
Why?
doing
at global scope you define global variables and for the compiler the implicit type of ptr is int in the line
ptr = &val;
so this is not compatible with anint*
. You cannot have an assignment at global scope, this is whyptr = &val;
is not the assignment of ptr previously defined but the definition of a global variable with an initial value.Putting the code in a local scope there is no problem, for instance with
compile without problem