Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointer to array of unspecified size "(*p)[]" illegal in C++ but legal in C

I just found out that this is illegal in C++ (but legal in C):

#include <stdio.h> #include <stdlib.h> #define ARRAY_LENGTH(A) (sizeof(A) / sizeof(A[0]))  int accumulate(int n, const int (*array)[]) {     int i;     int sum = 0;     for (i = 0; i < n; ++i) {         sum += (*array)[i];     }     return sum; }  int main(void) {     int a[] = {3, 4, 2, 4, 6, 1, -40, 23, 35};     printf("%d\n", accumulate(ARRAY_LENGTH(a), &a));     return 0; } 

It compiles without problems using gcc -std=c89 -pedantic but fails to compile using g++. When I try to compile it using g++ I get these error messages:

main.cpp:5:37: error: parameter 'array' includes pointer to array of unknown bound 'int []'  int accumulate(int n, int (*array)[])                                      ^ main.cpp: In function 'int main()': main.cpp:18:50: error: cannot convert 'int (*)[9]' to 'int (*)[]' for argument '2' to 'int accumulate(int, int (*)[])'      printf("%d\n", accumulate(ARRAY_LENGTH(a), &a)); 

I have been using this in my C code for a long time and I had no idea that it was illegal in C++. To me this seems like a useful way to document that a function takes an array whose size is not known before hand.

I want to know why this is legal C but invalid C++. I also wonder what it was that made the C++ committee decide to take it away (and breaking this compatibility with C).

So why is this legal C code but illegal C++ code?

like image 937
wefwefa3 Avatar asked Jan 12 '15 08:01

wefwefa3


1 Answers

Dan Saks wrote about this in 1995, during the lead up to C++ standardisation:

The committees decided that functions such as this, that accept a pointer or reference to an array with unknown bound, complicate declaration matching and overload resolution rules in C++. The committees agreed that, since such functions have little utility and are fairly uncommon, it would be simplest to just ban them. Hence, the C++ draft now states:

If the type of a parameter includes a type of the form pointer to array of unknown bound of T or reference to array of unknown bound of T, the program is ill-formed.

like image 116
user2649908 Avatar answered Sep 22 '22 06:09

user2649908