I have a question regarding to the code snippet appended below. Anyway I ran the snippet on ideone.com and got two different results
- C: Succeed.
- C++: Error:
prog.cpp: In function ‘int main()’: prog.cpp:20:13: error: cannot convert ‘int* (*)[2][10]’ to \ ‘int* (*)[10]’ for argument ‘1’ to ‘void foo(int* (*)[10], size_t)’ foo(&a, LEN); ^
The result in C++ is what I expect, but it runs successfully in C, and it seems like it's compiler dependent because people on the chat helping ran the snippet only got a warning.
So which part I've missed? Is that C automatically did some conversion?
#include <stdio.h>
#include <stddef.h>
#define LEN 2
void foo(int* a[][10], size_t len) {
printf("%s\n", "successfully called foo.");
}
int main(void) {
// a is an LEN-array of an 10-array of (int *)
int *a[LEN][10] = {{0}};
// but the identifier `a` will decay to be a pointer of type int*[10]
// p1 is a pointer to an 10-array of (int *)
int *(*p1)[10] = 0;
foo(a, LEN);
foo(&a, LEN);
return 0;
}
error: incompatible pointer types passing 'int *(*)[2][10]' to parameter of type 'int *(*)[10]'.