c - Initialize a struct -


i 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. first had pointer on role , assigned address of variable. removed pointer because don't need , can t assign value variable. 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 c99 standard §6.7.8 ¶4 says

all expressions in initializer object has static storage duration shall constant expressions or string literals.

also, const not true constants in c in sense not compile-time constant. means cannot have constant object in initializer of structure has static storage allocation. however, if structure has automatic storage allocation, work fine.

what can define const objects macros -

#define resource01url "/dummy" #define resource01role 2 

Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -