3

I'd like to shorten variable names, so instead of this : FPData.Temps.T.Solar.Val

I'd like to use :

TEMP_Solar.Val

and define macro :

#define TEMP_  FPData.Temps.T.

But it works only if I put space in between :

TEMP_ Solar.Val     

compiles ok, but I'd like to use this one

TEMP_Solar.Val

Possible? I know I could get around by using macro and arguments "TEMP_VAL(Solar)" but would like to keep it simple, linear concatenation...

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
user2152780
  • 53
  • 2
  • 6
  • Not sure that it is possible. What should the preprocessor do if you will write something like `_TEMP_DEFINE_`? – Alex Oct 22 '13 at 14:11
  • I think the '##' is the only way to make new identifiers/tokens in C or cpp or C++ source. – vrdhn Oct 22 '13 at 14:11
  • 1
    don't forget to _upclick_, **or** click the _answered_ check by any of these responses that work for you. – ryyker Oct 22 '13 at 14:18
  • Why not use pointer instead? my_val_type *pval = &FPData.Temps.T.Solar.Val; – KBart Oct 23 '13 at 07:55

2 Answers2

3

It's because the preprocessor, which handles macros, only recognizes their own identifiers. When you use e.g. TEMP_Solar it's a different identifier from TEMP_.

The preprocessor might even use a simple strcmp to find its macros, so there can't be no sub-strings nor can there be differences in case.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

The most obvious and easy solution:

#define TEMP FPData.Temps.T

TEMP.Solar.Val

(You cannot and should not change the actual variable names of the struct members.)

Lundin
  • 195,001
  • 40
  • 254
  • 396