可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'm currently learning function that work with multi-dimensional array in C.
Let's say I have
short myArray[10][30].
I have declared following function prototype to work with myArray
.
void first(int n, short ar[][30]); void second(int n, short ar[30]); void third(int n, short (*ar)[30]);
My understanding is that the ar
parameter in both first
and third
function are equivalent. short ar[][30]
in first
function is equivalent to the short (*ar)[30]
in third
function because they are both pointer to array of 30 short
.
The ar
in second
function is different because in short ar[30]
, ar
is a pointer to short
, not a pointer to array of 30 short
.
Is my understanding correct?
回答1:
Yes your understanding is correct. 1 and 3 are equivalent. And 2 is also right (But not for passing 2d array - it is correct for passing 1D array). But will clarify a bit the second case.
And the second one that 30 inside of third brackets are not considered by the compiler. You can omit it still the compiler won't complain. Actually here you have passed an 1D array of short
that decayed into pointer to the first element (First element being short
it is short*
). So the second one you can also write as short *ar
.
void second(int n, short ar[]); void second(int n, short* ar );
These two works and they are equivalent in this context. The second one is for passing 1D array something like
second(n, myArray[5]);
The thing is, most of the time array decays into pointer (exception is sizeof
operator or Alignof
etc). Passing an array to a function is a case where the array decays.
Also you are passing int
arrays so it is wrong to write short
.(int
and short
may have same size but it is guaranteed that size of int
would be larger than or equal to the size of short
). If you used short
and then wrote int
in the declaration that would have worked.
Edit: The second one is not for passing 2d array. Let's be clear on that. You can't pass 2d array to a function with the prototype declared as the second one. For pointers there are 2 things to consider - it's type and it's value. If you tried to pass a 2d array to the same function that would be illegal. 2d array decays into int (*)[30]
which is not in anyway same as int *
or int[]
.
回答2:
1 and 3 are indeed the same, as would be
void fourth(int n, short ar[10][30]);
because when you pass an array as function parameter, it decays to a pointer to its first parameter, so the compiler sees 1 and 4 as 3.
That explains why this would also be correct:
void fifth(int n, short arr[15][30]);
As it decays to a pointer, the declared size of the first dimension is not used. You are supposed to give the actual size in another way.
But this one is different:
void second(int n, short ar[30]);
and your compiler should raise a warning there because the expected paramater is a pointer to short
, when you pass a pointer to an array of 30 short
. Of course the pointers will have same value (same address), and common compilers will give expected results, but aliasing a pointer to array and a pointer to element is not allowed by the standard. So please avoid it.
With such a declaration, second
should be called as
cr = second(n, arr[0]);
because arr[0]
is a short array and will correctly decay to a short *
.