我必须为静态变量分配一个我从函数中获取的值.我试着做以下但我得到的初始化元素不是常数.
int countValue()
{
return 5;
}
void MatrixZero()
{
static int count=countValue();
count++;
printf("count value %d \n",count);
}
int main()
{
MatrixZero();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
小智 12
因为......好吧......静态变量的初始值不是常量.它必须是一个恒定的表达.试试这个:
static int count = SOME_VALUE_OUT_OF_RANGE;
if (count == SOME_VALUE_OUT_OF_RANGE) {
count = countValue();
}
Run Code Online (Sandbox Code Playgroud)
检查它是否已经初始化.
static必须使用常量表达式初始化使用存储说明符声明的变量.
static int count=countValue();
Run Code Online (Sandbox Code Playgroud)
函数调用不是常量表达式.