3

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;
}
10
  • 2
    Yes, it's legal. Whether or not it's "correct", depends on what that function may do to the data. Commented Aug 10, 2017 at 5:45
  • 1
    When you use a variable in a function call, you don't really pass the variable itself, only it's value. Or rather, a copy of its value. Commented Aug 10, 2017 at 5:48
  • Why function2(ptr) and not function2(newPtr) directly? Commented Aug 10, 2017 at 5:50
  • 1
    @n.m. That's not really helpful advise. There are plenty of cases where static is 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. static file 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". Commented Aug 10, 2017 at 6:32
  • 1
    @n.m. I just gave you an example of when they are good practice. How would you then write a driver, for a hardware of which there only exists one instance, which contains an interrupt, where the driver functions need to communicate with the driver ISR internally? It sounds as if you are used to program very specific C applications and therefore assume that the kind of applications you do are how C should be done universally. It smells like Linux programming. Commented Aug 10, 2017 at 9:24

3 Answers 3

3

static specifier only limits the scope of the variable (internal linkage). But when you pass the ptr, the address contained in ptr will be used and that is completely legal (no problem in compilation, since you are not using the variable ptr, you are using the value contained in it).

But think twice before doing that since you declared as static if some other person looks at your code, its gives an impression that the variable is used only in this file. If the code in function2 does anything to the passed pointer (assume you have dynamically allocated memory to your pointer and it is freed in function2 and you tried to delete/access in your file where you declared ptr).

If you take care of what function2 is about to do with the pointer, then its completely safe to do so. But as I mentioned above, it is not a good practice to do so.

Sign up to request clarification or add additional context in comments.

Comments

3

Static variable

static Event * ptr = NULL;

cannot be seen from other source file, but if you pass it as argument, it is just copied on stack same as other pointers, so you can do it that way.

But i would pass it as const pointer and explicitly document it that it is static variable for others.

void function2(Event * const pointer)
{
    pointer->event = 2;
}

1 Comment

Why const and explicity document it? There is no reason for that.
2

You can do whatever you want with it. Static means global storage && local file symbol visibility. But variable can be used as you wish

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.