How can I do something like this?
void function(int n)
{
static int number = n;
.
.
.
}
If you want to initialize the static variable to n during the first invocation of the function, you can do it like this:
void function(int n)
{
static int initialized = 0;
static int number;
if (!initialized) {
number = n;
initialized = 1;
}
.
.
.
}
You can't initialize number to n directly since number is initialized at compile time, while n is known only at run time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With