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

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

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

ソリューションへジャンプ
2,235件の閲覧回数
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?

ラベル(1)
タグ(1)
0 件の賞賛
返信
1 解決策
1,859件の閲覧回数
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 件の賞賛
返信
3 返答(返信)
1,859件の閲覧回数
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 件の賞賛
返信
1,860件の閲覧回数
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 件の賞賛
返信
1,859件の閲覧回数
FridgeFreezer
Senior Contributor I

Thanks Daniel, that does make sense.

0 件の賞賛
返信