From what I remember, arrays are always passed as pointers. For example, an ad:
void foo(int array[2][5]);
means for the compiler the same as:
void foo(int (*array)[5]);
You can say that both of these forms are equivalent. Now, I wonder why this is allowed to declare it as:
void foo(int (*array)[]);
not like:
void foo(int array[][]);
Take an example:
#include <stdio.h>
void foo(int (*p)[3]);
void bar(int (*p)[]);
int main(void)
{
int a[2][3] = {{1, 2, 3}, {4, 5, 6}};
foo(a);
bar(a);
return 0;
}
// The same as int p[][3] or int p[N][3] where N is a constant expression
void foo(int (*p)[3])
{
}
// Would it the same as int p[][] or int p[N][] (by analogy)?
void bar(int (*p)[])
{
}
It compiles fine and without warning, but if I change the declaration barto:
void bar(int p[][]);
then this is a mistake.
Why does C allow such an "obscure" way to pass an array?
source
share