What is the use case for a pointer to array of unknown size?

时间:2015-06-15 14:59:59

标签: c++ arrays pointers

I recently realized that C++ allows pointers to arrays of unknown sizes, such as

int (*p)[];

The declaration above is not equivalent to int* p;. In fact, trying to use p to point to an array of fixed size results in a compile-time error, e.g.

int data[42];
p = data; // error, cannot convert int [42] to int (*) []

or

int* data;
p = data; // error, cannot convert int * to int (*) []

My question is when and why should we use such a pointer to an array of unknown size? Are there any compelling reasons to do so, and not to use simply int* instead?

EDIT

Such objects are not allowed as function parameters, see Pointer to array of unspecified size "(*p)[]" illegal in C++ but legal in C, but can nevertheless be declared in the program.

0 个答案:

没有答案