I have a ptr variable that is updated in a function. I want to pass this variable to function2, which is in a different .c file.
Is it legal to pass a static variable to a function not in the same .c file? Would it be safer to just keep a global ptr without static keyword?
static Event * ptr = NULL;
void function(Event * newPtr)
{
ptr = newPtr;
function2(ptr);
}
//in separate c file
void function2(Event * pointer)
{
pointer->event = 2;
}
function2(ptr)and notfunction2(newPtr)directly?staticis perfectly fine. For example it can be used for private encapsulation purposes on single-core, single-threaded systems. For example, it is very commonly used that way in embedded systems, and it is entirely good practice. As opposed to using globals/extern, which is 100% bad practice on pretty much every kind of system.staticfile scope variables should only be avoided if there are multiple threads or multiple instances of objects using the static. It all depends on context, you can't say "always avoid it".