0

I would like to ask if there could be any problem in passing a static variable to a function by reference, in order to let that function modify the variable. A stupid example is:

void increment (int *b)
{
    *b = *b + 1;
}

void f()
{
   static int a = 4;
   increment(&a);
   printf("%d\n", a);
}

int main()
{
   f();
   f(); 
   f();
   return 0;
}

I executed it and it seems to work fine, printing

5
6
7

but I would like to understand if this is a sort of "bad" programming practice, that is preferable not to do.

200_success
  • 7,286
  • 1
  • 43
  • 74
E.Mac
  • 9
  • 1
  • 3
  • This question probably belongs on Code Review. – Kerrek SB Jan 13 '16 at 23:29
  • A few issues I can think of, the semantics are a little strange, and threading. – Justin Meiners Jan 13 '16 at 23:30
  • 1
    Yes, as @JustinMeiners says, it is not reentrant. I'm more bothered by having a function who's sole purpose is side effects (function "f"). I'd like a more realistic example before I'd say it's "bad" in general. – Lee-Man Jan 13 '16 at 23:32
  • ok: in the project I am working on, in different parts of the function "f()" I have to reset some static variables declared in "f()" to a predefined value. And so in order to let my code more understandable I want to put this "resetting" of the variables inside another function. Maybe another idea is to use a macro... – E.Mac Jan 13 '16 at 23:39
  • For that kind of use, you may as well define a global variable; the semantics are the same (single instance accessed from multiple parts of the code) and at least it will be clearer to whoever reads the code. If you have multiple variables, try to at least put them together in a struct to limit the damage and make it clear when you are operating with the global variable. – SJuan76 Jan 13 '16 at 23:40
  • 2
    @KerrekSB "A stupid example" doesn't make good code for reviewing. It would be off-topic on Code Review. – 200_success Jan 13 '16 at 23:40
  • I do not see any problem which would not exist when you are not using it. Using static (non-const) variables can cause problems with multithreading, but this problem would exists even without using such function. – JojOatXGME Jan 13 '16 at 23:49
  • 2
    Strictly speaking, [C does not support pass by reference](http://stackoverflow.com/questions/2229498/passing-by-reference-in-c). You have to pass a pointer **by value** that refers to something else. – Andrew Henle Jan 14 '16 at 00:48
  • 2
    Saying C does not support pass-by-reference is like the old argument (which many of you might not remember) about whether, in a proper object-oriented language, objects communicate by sending messages or by calling functions. Logically and pragmatically, there was no difference. Fortran passes arguments by reference by passing a pointer to the argument. In C, passing a pointer to an argument is effectively passing the argument by reference. (If you're feeling Algolish, you can also do pass-by-name by passing a function pointer for the thunk.) – Alex Measday Jan 14 '16 at 02:47
  • This is fine imo. The comments about threading would apply to any use of a static variable. You can use a thread-local variable if you want. – M.M Jan 14 '16 at 05:39
  • @AlexMeasday, while the final effect may be very similar, the resulting source code is significantly different between passing by reference and passing the address of some object by value. Please do not confuse the two methodologies – user3629249 Jan 14 '16 at 17:05
  • 1
    Possible duplicate of [Passing pointer to static global variable in C](http://stackoverflow.com/questions/22407964/passing-pointer-to-static-global-variable-in-c) – Ani Menon Apr 29 '16 at 04:19

0 Answers0