Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access a static variable from another file in C? [duplicate]

Possible Duplicate:
Static variable

How to access a static variable from another file in C? As a Static variable has a file scope, I think there is no way we can access it outside a file. But still I feel there might be some trick or way to do the same.

like image 275
aks Avatar asked Feb 09 '10 12:02

aks


1 Answers

I don't think there is a easy way. If you can change the file with the static variable you can do something like:

static int hiddenVar; // The static var you want to get at

// A new function you write
int * getHiddenVar() {
   return &hiddenVar;
}

But of course if you can change the file, you would just drop the static keyword.

Also, I doubt this helps, but I've had to do something like this when writing a kernel module in FreeBSD. I used a trick where I called the kernel's linker functions to find the address of a static function. I doubt you can do this in a normal C program though.

like image 99
bramp Avatar answered Oct 05 '22 02:10

bramp