How to Initialize char array from a string

后端 未结 11 1997
时光说笑
时光说笑 2020-12-14 09:19

I want to do the following

char a[] = { \'A\', \'B\', \'C\', \'D\'};

But I do not want to write these characters separately. I want somethi

11条回答
  •  天涯浪人
    2020-12-14 09:52

    Here is obscure solution: define macro function:

    #define Z(x) \
            (x==0 ? 'A' : \
            (x==1 ? 'B' : \
            (x==2 ? 'C' : '\0')))
    
    char x[] = { Z(0), Z(1), Z(2) };
    

提交回复
热议问题