How to share a variable between two functions in C?

后端 未结 6 1848
抹茶落季
抹茶落季 2021-01-22 16:27

In C, suppose var1 is a variable in foo1() and foo2() wants to access var1, however, foo1() doesn\'t call foo2(), so we can\'t pass it by parameter. At the same time, only foo1(

6条回答
  •  失恋的感觉
    2021-01-22 17:16

    No. The variable only exists on the function stack while foo1() is running. The stack will vanish when leaving the function. You could make the variable static to keep it alive, but then you can't access it from the outside either without hacks.

提交回复
热议问题