user968000
user968000

Reputation: 1843

Initializer element is not a constant

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

Answers (3)

user3809800
user3809800

Reputation: 1

// wenn countValue ein Objekt zurückgibt

static int* count=0;  if(count==0)count=countValue();

Upvotes: -1

user529758
user529758

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

ouah
ouah

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

Related Questions