Say I declare a function passing array as a pointer:
void fun(int *A, int n);
If I use an array specific pointer instead, i.e.
void fun(int A[], int n);
Is there any particular advantage to it?
Say I declare a function passing array as a pointer:
void fun(int *A, int n);
If I use an array specific pointer instead, i.e.
void fun(int A[], int n);
Is there any particular advantage to it?
(This answer speaks to C.)
For an int array with size [], there are no differences to the compiler, because C 2018 6.7.6.3 7 says:
A declaration of a parameter as "array of type" shall be adjusted to "qualified pointer to type", where the type qualifiers (if any) are those specified within the
[and]of the array type derivation…
However, for some type T in general, there is a difference. Per C 2018 6.7.6.2 1, in a declaration of an array, the element type must be complete, even if the array will be adjusted to a pointer later:
… The element type shall not be an incomplete or function type…
Consider typedef int T[];, which defines T to be an alias for int [], which is an array of int with an unknown number of elements. This type is incomplete because the number of elements is unknown. So we cannot declare an array of T. In contrast, a pointer may point to an incomplete type. So void fun(T *A, int n); is allowed, but void fun(T A[], int n); is not allowed because it violates 6.7.6.2 1.
Another difference is if the size is specified and is variable. Then it may be evaluated when the function is called. (The C standard is not explicit about this, but Clang and GCC evaluate it.) For example, given a function definition void fun(int A[printf("Hello, world.\n")], int n) { … }, when fun is called, “Hello, world.” will be printed. This of course does not happen with void fun(int *A, int n) { … }.