Array Equivalent of Bare-String

2019-03-06 08:36发布

问题:

I can do this without issue:

const char* foo = "This is a bare-string";

What I want is to be able to do the same thing with an array:

const int* bar = {1, 2, 3};

Obviously that code doesn't compile, but is there some kind of array equivalent to the bare-string?

回答1:

You can't do this:

const int* bar = {1, 2, 3};

But you can do this:

const int bar[] = {1, 2, 3};

Reason is that char* in C (or C++) have an added functionality, besides working as a char pointer, it also works as a "C string", thus the added initialization method (special for char*):

const char* foo = "This is bare-string";

Best.