Maybe looking at the function from the side of the function prototype you will see more sense of this:
int f(int[], int n);
Here f()
is declared to return an int
and use two arguments: the first is an address of an array of int
, the second is an int
.
Since inside f()
you may want to know how many int
's are in the vector, it is common to the second argument to be number of elements in the array. Just as an example. Any similarity with the pair
int argc, char** argv
is because it is the same thing: the system builds a list of the arguments passed on the command line and constructs the argv
array. And passes argc
as the number of arguments in the array.
C does pointer arithmetic, in a sense that if x
points to an int
(x+1)
points to the x
plus the sizeof()
an int
. This is fantastic in order to abstract things like memory, registers and hardware.
In this way, when you call f()
again passing 1 + the original argument you are just calling f()
and passing the array beginning at the next position.
Back to your example, consider f()
just
int f(int nums[], int n)
{
printf("First int is %d\n", nums[0]);
return 0;
}
where f()
just writes down the first int of the block, and the code
int f(int[], int n);
int main(int arg, char** argv)
{
const int one[4] = { 1,2,3,4 };
const int other[2] = { 3,4 };
f(one, 4);
f(other, 2);
f(other + 1, 3'000);
return 0;
};
it shows
First int is 1
First int is 3
First int is 4
And you see that in the 3rd call f()
gets the array other starting at 4
, the second element.