7

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.

Community
  • 1
  • 1
vsoftco
  • 55,410
  • 12
  • 139
  • 252
  • @MarcoA. That only talks about their (illegal) use as function parameters, doesn't it? The construct itself seems still to be part of C++. – Baum mit Augen Jun 15 '15 at 15:08
  • @MarcoA. Weirdest thing is that the declaring such a thing compiles with no warnings whatsoever, `-Wall -Wextra -pedantic`, by both clang/gcc. And yes, they cannot be used as function parameters, but can nevertheless be defined. I made a note in the question, thanks for the link. – vsoftco Jun 15 '15 at 15:08
  • Related: http://stackoverflow.com/questions/21589449/what-is-flexarr-and-how-why-do-c-programmers-use-it – erenon Jun 15 '15 at 15:12
  • 1
    Ah here it is: http://stackoverflow.com/q/24478830/1938163 – Marco A. Jun 15 '15 at 15:13
  • @MarcoA. Thanks. Sorry, I thought it had been closed as a duplicate of the other one and reopened it before closing it again. – Potatoswatter Jun 15 '15 at 15:16
  • @MarcoA. thanks for digging in and finding the dupe. In terms of usability, I don't really find any compelling reasons to use such a construct though... – vsoftco Jun 15 '15 at 15:17
  • @Potatoswatter no problem at all! vsoftco I'd agree with you – Marco A. Jun 15 '15 at 15:18
  • @vsoftco I was going to add an answer that the current feature is basically a stub pending addition of a standard pointer conversion from "pointer to array of known bound" to "pointer to array of unknown bound." It's my bedtime, so I'll just leave it at that. There's a paper or defect report somewhere if you search hard enough. – Potatoswatter Jun 15 '15 at 15:19

0 Answers0