0
#include <stdio.h>

void func() {
   static int x = 0;  // x is initialized only once across three calls of
                      //     func()
   printf("%d\n", x); // outputs the value of x
   x = x + 1;
}

int main(int argc, char * const argv[]) {
   func(); // prints 0
   func(); // prints 1
   func(); // prints 2

   // Here I want to reinitialize x value to 0, how to do that ? <-- this line
   return 0;
}

In the above code, after calling func() 3 times I want to re-initialize x to zero. Is there any method to reinitialize it to 0?

jogojapan
  • 68,383
  • 11
  • 101
  • 131
Anand
  • 67
  • 4
  • 9
  • 1
    You could declare `func()` so it takes an argument that tells it whether or not to re-initialise. E.g. `void func(const bool reset=false) { ... }`. – jogojapan Oct 29 '12 at 07:20
  • @jogojapan well, technically you could declare a global int pointer and set it to x.. but that sounds undesirable – Ancurio Oct 29 '12 at 07:26
  • @Ancurio Yes, of course there various ways to accomplish this. The reason why I suggested the function parameter is that it keeps the ultimate authority over the value of `x` with `func`, which is appropriate, given that it's a local static variable. – jogojapan Oct 29 '12 at 07:29

3 Answers3

2

Do you want the function always to reset the counter after three invocations? or do you want to the caller to reset the count at arbitrary times?

If it is the former, you can do it like this:

void func() {
  static int x = 0;
  printf("%d\n", x);
  x = (x + 1) % 3;
}

If it is the latter, using a local static variable is probably a bad choice; you could instead use the following design:

class Func
{
  int x;
  // disable copying

public:
  Func() : x(0) {}

  void operator() {
    printf("%d\n", x);
    x = x + 1;
  }

  void reset() {
    x = 0;
  }
};

Func func;

You should make it either non-copyable to avoid two objects increasing two different counters (or make it a singleton), or make the counter a static member, so that every object increments the same counter. Now you use it like this:

int main(int argc, char * const argv[]) {
  func(); // prints 0
  func(); // prints 1
  func(); // prints 2

  func.reset();
  return 0;
}
Andrzej
  • 5,027
  • 27
  • 36
1

You can have func() assign the address of the variable to a pointer that's visible from outside func().

Or you can have a special parameter you can pass to func() to tell it to reset the variable.

But really, the whole point of declaring x inside func() is to make it visible only within the body of func(). If you want to be able to modify x, define it somewhere else. Since you're writing C++, it probably makes sense for x to be a static member of some class.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
0

You can't. It's a local static variable which is invisible outside func().

You can either use:

  1. Global static (not recommended generally).
  2. Pass it through reference/pointer parameter, or return by reference.
jogojapan
  • 68,383
  • 11
  • 101
  • 131
Eric Z
  • 14,327
  • 7
  • 45
  • 69