Illegal constant expression. in MACRO?

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Illegal constant expression. in MACRO?

1,409件の閲覧回数
Ali2006
Contributor II

Trying to use the following macro to create an offset into a structure. 

 

When I try to compile under CW10.2 I get illegal constant expression.

I have used this before with other compilers.. so we are stumped. Any ideas greatly appreciated

 

#define FIELD(type, field) {type, (((char*)&(Config. ## field)) - (char*)&Config), sizeof(Config. ##field)}

 

 

USAGE in creation of this table..

static const Command_t Cmds[] = {

// Cmd Function, {Field 1, Field 2}, ResponseCount, TestDiff,Help, Comment

{

{"DL", &cfOScope, { FIELD(dHex,Debug.Output), NO_FIELD }, 0, true, HELP("D?", "Debug LED Outputs (output num, hex event mask)" )},

{"DL?", &cfOScopeList, { NO_FIELD, NO_FIELD }, 0, false, HELP("D?", "Debug LED Output Help Listing" )},

{"DS", NULL, { FIELD(dHex,Debug.Stream), NO_FIELD }, 1, true, HELP("D?", "Debug Stream Mask (hex mask)")},

{"DSS", &cfDbgStreamSingle, { NO_FIELD, NO_FIELD, }, 0, false, HELP("D?", "Debug Stream Single Listing (hex mask)" )},

 

};

 

 

The following macro works just fine... I don't really see the difference

 

#define DoSetConfig(field, valPtr, valSize) \

SetConfig(( char*)&Config. ## field - (char*)&Config,\

(char*)(valPtr), (valSize))

ラベル(1)
0 件の賞賛
返信
2 返答(返信)

604件の閲覧回数
bigmac
Specialist III

Hello,

 

I do not understand your macro, but there seems to be an inconsistency whether there is a space following the ## symbol, or not.

 

Regards,

Mac

 

0 件の賞賛
返信

604件の閲覧回数
CompilerGuru
NXP Employee
NXP Employee

I'm not sure why the preprocessor concatenation operation ## is used, it does not seem necessary.

 

Anyhow, for getting the offset of a field in a struct, I would recommend to use the ANSI C offsetof macro.

For understanding what in the original code is not working, I would need a complete compilable sample, in the given snippet there is just too much I can only guess.

Also please add which target/derivative/compiler this is built with as MCU 10.2 supports many different processors.

 

The first thing I look at when having such a failure is the preprocessor output. It more clearly shows what is going on.

 

Daniel

 

BTW: Note that the DoSetConfig macro seems to evaluate to a function call. For a function call arguments do not need to be constant, so could be that the issue is not an illegal syntax, but that for the compiler the used expression is not a compile/link time constant.

 

0 件の賞賛
返信