In C passing by reference means passing an object indirectly through a pointer to it. Thus dereferencing the pointer you will get a direct access to the object.
When an array designator is passed to a function it is implicitly converted to a pointer to its first element. From the C Standard (6.3.2.1 Lvalues, arrays, and function designators)
3 Except when it is the operand of the sizeof operator or the unary &
operator, or is a string literal used to initialize an array, an
expression that has type ‘‘array of type’’ is converted to an
expression with type ‘‘pointer to type’’ that points to the initial
element of the array object and is not an lvalue. If the array object
has register storage class, the behavior is undefined.
On the other hand, a parameter having an array type is adjusted by the compiler to pointer to the element type of the array. From the C Standard (6.7.6.3 Function declarators (including prototypes))
7 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.
If the keyword static also appears within the [ and ] of the array
type derivation, then for each call to the function, the value of the
corresponding actual argument shall provide access to the first
element of an array with at least as many elements as specified by the
size expression.
So this function declaration
void Size(int A[], int size);
is equivalent to
void Size(int *A, int size);
As a result all elements of the array in fact passed by reference through the pointer to the first element of the array. Using the pointer arithmetic you can get a direct access to any element of the array.
Pay attention to that the type of an expression with the sizeof
operator is size_t
. So you have to use the conversion specifier %zu
instead of %d
in the call of printf
.
printf("Size of A = %zu\nSize of A[0] = %zu\n\n", sizeof(A), sizeof(A[0]));
In this call sizeof( A )
yields the size of the pointer A due to the implicit conversion of the passed array to pointer to its first element and due to adjusting the function parameter to pointer to the array element type by the compiler.