开发者

C/C++: Constant array of constant arrays

What would be the syntax for creating a constant array of constant arrays?

I am wanting a function argument to be a c开发者_开发百科onstant array of constant char* strings.


You do this by putting const on the right of the first asterisk, e.g.

void f(const char *const *argument)

or equivalently

void f(const char *const argument[])

For more dimensions, simply add more *consts (I would not use the [] alternative in this case):

void f(const char *const *const *argument) // 2D array of strings


The key to this is to write the C++ backwards (right to left):

         char * const myVar[10] const;

...which says that myVar is an const array length 10 of const pointer to char.


I believe that would be a

const char* const array[size][size] = { /* initializer */ }

That is, an array of arrays of immutable pointers to characters that can't be changed.


The question is a bit unclear: Do you want to create (define) an array, or pass it to a function?

The syntax to define a constant array of constant C-strings is

const char array[2][14] = { "first string", "second string" };

To define a constant array of constant arrays of non-string type, the initializer differs:

const int array[2][3] =
{
  { 1, 2, 3 },
  { 4, 5, 6 }
};

(If it's appropriate, you should make the array static const.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜