Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2017-10

Initializing constant (translate from C)

3 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Darius Blaszyk

Here's an interesting one. In C I have this code:

#define ID1 MAKE_ID('A', 'B')

Where MAKE_ID is a macro that depending on the endianness of the target will create either AB or BA.

How would I be able to translate this into Pascal? ID1 preferably would need to remain a constant. I tried using an inline function without success. If all fails I will need to define them all individually using an ifdef for different endian systems. But this would be my last resort.

TIA, Darius

Dmitry Boyarintsev
to Darius Blaszyk
Darius Blaszyk wrote 8 lines
Here's an interesting one. In C I have this code: #define ID1 MAKE_ID('A', 'B') Where MAKE_ID is a macro that depending on the endianness of the target will create either AB or BA. The only issue with the following approach
const
  ID  ={$IFDEF ENDIAN_LITTLE} 'AB' {$ELSE} 'BA' {$ENDIF};

is if MAKE_ID is used anywhere else, you'll have to repeat the code.

thanks,
Dmitry

Sven Barth
to Darius Blaszyk: Here's an interesting one. In C I have this code: #define ID1 MAKE_ID('A', 'B') Where…
"Darius Blaszyk" wrote 6 lines
Here's an interesting one. In C I have this code: #define ID1 MAKE_ID('A', 'B') Where MAKE_ID is a macro that depending on the endianness of the target

will create either AB or BA.

How would I be able to translate this into Pascal? ID1 preferably would

need to remain a constant. I tried using an inline function without success. If all fails I will need to define them all individually using an ifdef for different endian systems. But this would be my last resort.

Best use inline functions for both ID1 and MAKE_ID. The compiler should optimize away the calls and do a constant concatenation.

Regards,
Sven