0

I would want to initialize char array during compilation time with least amount of manual work.

Is there a working shorthand format for this

char arr[5] = {0x4, 'a', 's', 'd' 'c'};

such as

char arr[5] = {0x4, "asdc"};
1
  • Isn't this an XY problem? Why would you want to have that arr? Shouldn't it be const if you want to initialize it at compile time? Commented Aug 2, 2017 at 11:42

2 Answers 2

1

You could integrate the char int the string with escape sequences:

char arr[6] = { "\x04asdc"};

edit: corrected the wrog length of the array.

Sign up to request clarification or add additional context in comments.

2 Comments

The problem with this is that the string "\x04asdc" is six characters long. Specifying a to small array size is incorrect.
@Someprogrammerdude please disregard my previous comments, I accidentally ended up on a C page. The nul-terminator is indeed explicitly mandated in C++.
1

No that's not possible. But you could do

char arr[] = "\04asdc";

The problem with this is that is would not be exactly like the original array you show, since it would include the string terminator and therefore have six elements.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.