0

Basically I want to split single token to multiple tokens enclosed in single quotes but as this seems impossible I've stopped on this. Basically:

#include <boost/preprocessor/seq/enum.hpp>

char string[] = {BOOST_PP_SEQ_ENUM((F)(l)(y)(O)(f)(f)(L)(e)(d)(g)(e))};

However how could I add the single quotes in?

Justin
  • 24,288
  • 12
  • 92
  • 142
AnArrayOfFunctions
  • 3,452
  • 2
  • 29
  • 66

2 Answers2

2

I don't believe it's possible to create character literals in standard compliant C, see C preprocessor: How to create a character literal? .

However, if you just want characters, you do have a few options:

  • You can expand it to a string literal with BOOST_PP_STRINGIZE and BOOST_PP_SEQ_CAT:

    char string[] = BOOST_PP_STRINGIZE(
        BOOST_PP_SEQ_CAT((F)(l)(y)(O)(f)(f)(L)(e)(d)(g)(e)));
    // Equivalent to:
    char string2[] = "FlyOffLedge";
    

    Live on Godbolt

  • You can expand each character to "c"[0]:

    #define TO_CSV_CHARS_OP(s, data, elem) BOOST_PP_STRINGIZE(elem)[0]
    #define TO_CSV_CHARS(seq) \
        BOOST_PP_SEQ_ENUM(BOOST_PP_SEQ_TRANSFORM(TO_CSV_CHARS_OP, , seq))
    
    char string[] = {
        TO_CSV_CHARS((F)(l)(y)(O)(f)(f)(L)(e)(d)(g)(e))
    };
    // Equivalent to:
    char string2[] = {
        "F"[0],
        "l"[0],
        "y"[0],
        "O"[0],
        "f"[0],
        "f"[0],
        "L"[0],
        "e"[0],
        "d"[0],
        "g"[0],
        "e"[0]
    };
    

    Live on Godbolt

Justin
  • 24,288
  • 12
  • 92
  • 142
1

You can adapt this answer in the linked question to C pretty easily to accomplish the original goal (live example):

#include <boost/preprocessor/repetition/repeat.hpp>
#include <boost/preprocessor/punctuation/comma_if.hpp>

#define GET_CH(s, i) ((i) >= sizeof(s) ? '\0' : (s)[i])

#define STRING_TO_CHARS_EXTRACT(z, n, data) \
        BOOST_PP_COMMA_IF(n) GET_CH(data, n)

#define STRING_TO_CHARS(STRLEN, STR)  \
        BOOST_PP_REPEAT(STRLEN, STRING_TO_CHARS_EXTRACT, STR)

char string[] = {STRING_TO_CHARS(12, "FlyOffLedge")};

I don't think it's possible in C to handle the length automatically.

If all you're after is the question as asked, you can use a trick like in Justin's answer to get the first character of each stringized character without using character literal syntax (similar live example).

chris
  • 60,560
  • 13
  • 143
  • 205