0

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?

4

1 回答 1

1

你不能这样做:

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

但是你可以这样做:

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

原因是 C(或 C++)中的 char* 具有附加功能,除了用作 char 指针外,它还用作“C 字符串”,因此添加了初始化方法(专用于 char*):

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

最好的。

于 2015-04-01T15:41:29.553 回答