How to #define a string of non-printable characters? (eg as array {0xnn,0xnn, 0xnn} etc.)

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to #define a string of non-printable characters? (eg as array {0xnn,0xnn, 0xnn} etc.)

Jump to solution
2,232 Views
FridgeFreezer
Senior Contributor I

I'm sure there's a simple way of doing this but CW (7.2 for Coldfire) keeps throwing errors "Illegal implicit onversion from 'char' to 'const char*' when I try to compile this code.

 

Basically we have a text display where users set a bit-field like "01011101", currently it selects the on/off characters from this constant string:

const char    *ascii_bits         = "01";

 

Because I want to change it to a couple of #defined special characters (not ASCII printable) to give a display of "blocks" like "_#__###_" we have set in the display device's charset, I can't enter the string as "XY", so I thought I'd do it array style:


#define BLOCKS_ON            (char)0xEE
#define BLOCKS_OFF         
(char)0xF2

const char    *ascii_blocks        = { BLOCKS_OFF, BLOCKS_ON, NULL}; // include string terminator (null)

 

But CW doesn't like this and throws an error. Can anyone tell me the correct way to go about this?

Labels (1)
0 Kudos
Reply
1 Solution
1,856 Views
CompilerGuru
NXP Employee
NXP Employee

unless you intend to change the ascii pointer, it should be const:

 

const char* const ascii_blocks        = "\0xF2\0xEE";

 

The string literal syntax does not mean you cannot use macros, just the macros need to be strings too:

#define AAA "\0xF2"

#define BBB "\0xEE"

const char* const ascii_blocks = AAA BBB;

The {} syntax can only be used to initialize structs or arrays, not pointers. Therefore you could write your initial sample as

#define BLOCKS_ON            ((char)0xEE)
#define BLOCKS_OFF          (
(char)0xF2)

const char ascii_blocks[]        = { BLOCKS_OFF, BLOCKS_ON, '\0'}; // include string terminator ('\0')

If you need a pointer, just make it point to an array defined this way.
Also wondering where there is a terminating 0 character at the end, if you index into ascii_blocks only using indexes 0 and 1, then there is no need for it.
Daniel

View solution in original post

0 Kudos
Reply
3 Replies
1,856 Views
FridgeFreezer
Senior Contributor I

Just to add, I know I can do this:

const char    *ascii_blocks        = "\0xF2\0xEE";

But I would like to retain the ability to link the string contents to the #defines.

0 Kudos
Reply
1,857 Views
CompilerGuru
NXP Employee
NXP Employee

unless you intend to change the ascii pointer, it should be const:

 

const char* const ascii_blocks        = "\0xF2\0xEE";

 

The string literal syntax does not mean you cannot use macros, just the macros need to be strings too:

#define AAA "\0xF2"

#define BBB "\0xEE"

const char* const ascii_blocks = AAA BBB;

The {} syntax can only be used to initialize structs or arrays, not pointers. Therefore you could write your initial sample as

#define BLOCKS_ON            ((char)0xEE)
#define BLOCKS_OFF          (
(char)0xF2)

const char ascii_blocks[]        = { BLOCKS_OFF, BLOCKS_ON, '\0'}; // include string terminator ('\0')

If you need a pointer, just make it point to an array defined this way.
Also wondering where there is a terminating 0 character at the end, if you index into ascii_blocks only using indexes 0 and 1, then there is no need for it.
Daniel

0 Kudos
Reply
1,856 Views
FridgeFreezer
Senior Contributor I

Thanks Daniel, that does make sense.

0 Kudos
Reply