AnsweredAssumed Answered

macro evaluation in compiler

Question asked by Ron Wilson on Jul 6, 2010
Latest reply on Jul 9, 2010 by bigmac

I am currently using "ANSI-C/cC++ Compiler for HC12 V-5.0.40 Build 10020, Jan 21 2010".

 

In my code, I am using this macro:

Mk4Bytes(x) { (((u8)((x) >> 24)) , ((u8)((x) >> 16)) , ((u8)((x) >> 8)) , ((u8)(x))) }

 

which I have used successfully in other compilers. However, when I used it to initialize a field in a structure, I got the error "ERROR C2207: Initializer must be constant"

 

Here is a excerpt of my code:

 

typedef unsigned char u8;

typedef struct {

u8 partnumber[4];

u8 version[2];

}  tIDheader;

 

#pragma CONST_SEG IDHEADER

const tIDheader IDheader = {

Mk4Bytes(12345678),

"A0"

};

 

While I know that I could simply change the field definition to use "unsigned long" instead of an array of bytes, the struct is actually defined in automatically generated code, so I am not allowed to change the definition.

 

Anyway, the value given to the macro is a constant so the resulting expression is a constant expression, so I would expect the compiler to replace the expression with { 0x00, 0xBC, 0x61, 0x4E }, which is a constant initializer.

 

As I said, I have used this macro successfully with other compilers. Is this a bug with this compiler?

Outcomes