With expressions, expressions with rare exceptions are implicitly converted to a pointer to their first element. So when you pass an array to a function, as in the example below.
void f( int *a, size_t n ) { for ( size_t i = 0; i < n; i++ ) printf( "%d ", a[i] ); printf( "\n" ); } int a[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; f( a, 10 );
it is not necessary to specify the & operator before the array name. The array has already been converted to an int * type pointer.
However, if you are dealing with scalar objects, such as the expression a[0] , which is the first element of the array, that is, a scalar object with a value of 0, as follows from the array definition in the example above, then you want to send its address to the above function, then you will need to write
f( &a[0], 10 );
In fact, these two calls
f( a, 10 );
and
f( &a[0], 10 );
equivalent, since in both cases the address is transferred to the first element of the array.
In view of this, these function declarations are equivalent and declare the same function.
void f( int a[10], size_t n ) void f( int a[20], size_t n ) void f( int a[], size_t n ) void f( int *a, size_t n )
You can include all these announcements in the program at the same time, and the program will be successfully compiled.
The same is true for character arrays. Keep in mind that string literals also have an array type. And if there is a call of the form
h( "Hello" );
where h is a certain function, then the address of the first character of the literal is transferred to the function, since, as described above, this literal, which is a character array, is implicitly converted to a pointer to its first character.
In conclusion, I will give a demo program, which also includes an example showing that string literals are arrays.
#include <stdio.h> void f( int a[10], size_t n ); void f( int a[20], size_t n ); void f( int a[], size_t n ); void f( int *a, size_t n ); void f( int *a, size_t n ) { for ( size_t i = 0; i < n; i++ ) printf( "%d ", a[i] ); printf( "\n" ); } void h( char c ) { printf( "%c\n", c ); } int main(void) { int a[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; f( a, 10 ); f( &a[0], 10 ); h( "Hello"[0] ); return 0; }
Output of the program to the console
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 H