-2
void foo() {    
    static int x{0};
    int y{0}; // this one
    // dosomething like ++y
    foo();    
}

Is it possible to have variable 'y' initialized only once(when foo called first time, not in each call) but separate independent local copy of it in each stack frame. while possible to change it in each stack frame(like ++y).

bni
  • 47
  • 4

1 Answers1

1

initialize only once non static local variable

By calling the function only once.

If you want a local variable initialised once in a function called multiple times, then that's what static local variables are for.

P.S. Your example function has infinite recursion. It will overflow the stack.

eerorika
  • 232,697
  • 12
  • 197
  • 326