The initializer element is not constant -> Objective C

I get the error "Initializer Element is not a constant" for the next declaration.

struct EBD_HEADER
{
    char   x;              
    int Key;     
    char Number;   
    int ID;       
    int Version;   
}EBD_HEAD;

struct EBD_TRAILER
{
    int Crc_0;            
    int Crc_1;           
    int  Etx;              
}EBD_TRAIL;

static const int EBD_HEADER_SIZE  = sizeof(EBD_HEAD);

static const int EBD_TRAILER_SIZE = sizeof(EBD_TRAIL);

static const int RMH_ENCODED_MSG_OVERHEAD = EBD_HEADER_SIZE + 
EBD_TRAILER_SIZE; //**error:Intializer Element is not a constant**
+3
source share
2 answers

Use macros instead.

#define EBD_HEADER_SIZE sizeof(EBD_HEAD)
#define EBD_TRAILER_SIZE sizeof(EBD_TRAIL)
#define RMH_ENCODED_MSG_OVERHEAD (EBD_HEADER_SIZE + EBD_TRAILER_SIZE)

Feels scared? Read this and remember that this is C (which Objective-C is based on), not C ++

+2
source

static const intin C declares a variable of the type intwhose value can be accepted (during optimization) not to be changed. It does not declare a constant, unlike C ++.

C, , , enum. , enum s; .

enum {
  EBD_HEADER_SIZE = sizeof (EBD_HEAD),
  EBD_TRAILER_SIZE = sizeof (EBD_TRAIL),
  RMH_ENCODED_MSG_OVERHEAD = sizeof (EBD_HEAD) + sizeof (EBD_TRAIL)
};

, enum ; , enum ( , typedef), , -.

, #define , - .

+2