Initializing a struct in C : initializer element is not constant -


i have read other answers on topic, didn't me. trying initalize struct getting following error msgs in c:

error: initializer element not constant

error: (near initialization 'resource01.resource.role')

for url works, it's role not working. , if i'm defining role char, don't have problems. doing wrong?

static char const resource01url[] = "/dummy"; static int const resource01role = 2; static struct restresourcenode_s resource01 = {     {         resource01url,         resource01role,         &dummyhandler_call     },     null };  static struct restresourcesmanager_s resourcesmanager = {     &resource01, &resource01 }; 

the type restresourcenode_s defined:

struct restresourcenode_s {     restresource_t resource;     struct restresourcenode_s const *next; } 

and restresource_t:

struct restresource_s {     char const *url;      int const *role;      retcode_t (*handle)(msg_t *); };  typedef struct restresource_s restresource_t; 

the error means using non-constant expression initialize structure member.

the expression resource01role.

while declared static , const not initializer constant expression view of c compiler. if want use way, have define preprocessor macro. in case, const points out compiler value of resource01role not change - not permit use value during compile-time.

however, @whozcraig pointed out, type of role int const *, meant write &resource01role. adress-of is constant expression, compile.

since resource01url array, adress-of operator & implicitly applied compiler, constant.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -