Reputation: 11
uint64 gameVarsAddress;
switch(version) {
case 100:
gameVarsAddress = 0x44500000;
break;
case 105:
gameVarsAddress = 0x4730000;
break;
}
static struct _gameVars *gameVars = (struct _gameVars *)(void*)gameVarsAddress;
when I use compile it give me error:
error: initializer element is not constant | 46
Upvotes: 0
Views: 185
Reputation: 223917
Variables with static storage duration, i.e. those declared either at file scope or with the keyword static
, can only be initialized with a constant expression. The value of a variable, even if const
, is not a constant expression.
If you use a macro for the value then you can do this. If you don't want gameVarsAddress
to be a macro you can still use one to initialize both gameVarsAddress
and gameVars
:
#define GAME_VARS_ADDRESS_DEFAULT 0x44500000
uint64 gameVarsAddress = GAME_VARS_ADDRESS_DEFAULT;
static struct _gameVars *gameVars =
(struct _gameVars*)(void*)GAME_VARS_ADDRESS_DEFAULT;
If on the other hand you want to set the value of gameVars
at runtime, you'll need to first initialize it to NULL then set the value later:
static struct _gameVars *gameVars = NULL;
if (!gameVars) {
// set gameVarsAddress
gameVars = (struct _gameVars*)(void*)gameVarsAddress;
}
Upvotes: 1