gpt4 book ai didi

c - 如何以更好的方式编写这些字符数组?

转载 作者:太空宇宙 更新时间:2023-11-04 05:48:41 26 4
gpt4 key购买 nike

现在我有一个 char 数组,我用它来存储字体数据:

const char * const FONT[] = {
"\x48" "a44448", //0
"\x27" "m\x48" "m\x40", //1
"\x06" "a46425" "m\x00" "m\x80", //2
"\x06" "a46425" "a42425", //3
"\x83" "m\x03" "m\x68" "m\x60", //4
"\x88" "m\x08" "m\x04" "m\x44" "a42424" "m\x00", //5
"\x02" "a42428" "a84842", //6
"\x08" "m\x88" "m\x20", //7
"\x44" "A46428" "a42428", //8
"\x86" "a46428" "m\x60", //9
...

有没有办法以更具可读性的方式编写它,但仍然在编译时计算它?例如,像这样的东西:

#define start(x,y) //somehow create '\x<x><y>'. start(3,4) -> '\x34'
#define arc(x,y,rx,ry,a) //evaluate to {'a','<x>','<y>','<rx>','<ry>','<a>'}. arc(1,2,3,4,5) -> {'a','1','2','3','4','5'}

const char * const FONT[] = {
start(4,8) arc(4,4, 4,4, 8) "", //somehow concatenate them
...

另外,为什么我可以使用字符串文字而不是字符数组文字:(这不起作用)

const char * const FONT[] = {
{'\x48','a','4','4','4','4','8','\0'}, //0

但这行得通:

const char X[] = {'\x48','a','4','4','4','4','8','\0'};
const char * const FONT[] = {
X,
...

最佳答案

这组宏应该做你想做的事:

#define str(s) #s
#define start(px,py) str(\x##px##py)
#define arc(x,y,rx,ry,pa) str(a##x##y##rx##ry##pa)

const char * const FONT[] = {
start(4,8) arc(4,4, 4,4, 8),
}

这利用了 # and ## operators (又名字符串化 resp。连接运算符)。

并导致以下预编译器输出:

const char * const FONT[] = {
"\x48" "a44448",
}

关于c - 如何以更好的方式编写这些字符数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50930169/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com