Reputation: 1843
I have to assign a static variable a value which I am getting from a function. I tried doing the following but I am getting initializer element is not constant.
int countValue()
{
return 5;
}
void MatrixZero()
{
static int count=countValue();
count++;
printf("count value %d \n",count);
}
int main()
{
MatrixZero();
return 0;
}
Upvotes: 6
Views: 7381
Reputation: 1
// wenn countValue ein Objekt zurückgibt
static int* count=0; if(count==0)count=countValue();
Upvotes: -1
Reputation:
Because... well... the initializer of your static variable is not a constant. It must be a constant expression. Try this:
static int count = SOME_VALUE_OUT_OF_RANGE;
if (count == SOME_VALUE_OUT_OF_RANGE) {
count = countValue();
}
to check if it has already been initialized.
Upvotes: 12
Reputation: 145829
A variable declared with the static
storage specifier must be initialized with a constant expression.
static int count=countValue();
a function call is not a constant expression.
Upvotes: 8