- Let's discuss in this segment
passing an array to a function.
An array is not a regular variable.
Obviously.
It's a sequence of data that's being
stored in one sequential location.
So let's see what is different about passing
an array from passing the regular variable.
First of all, passing an array.
It's not really passing the variable, but a pointer.
When we have an array, we cannot send the entire data set.
We are doing it in a more efficient way.
We are sending it the beginning location of the
data set, and the compiler will handle the rest.
Here's an interesting observation.
Function parameters are synonymous to pointers.
We will see that shortly in code.
But first, let's take a look here.
Let's say we have two functions.
Func1 and func2.
Notice the difference in the way
they define their one parameter.
The first one has an array of int.
The second one, func2, it's a pointer to an int.
Apparently, those are very different things.
Line three.
Let's initialize an array list with five elements
with the numbers one, two, three, four and five.
We are now going to pass the same array list to
both functions and see if there's a difference.
Notice that I am not passing the numbers.
I am not passing list five or list brackets.
I am just passing list.
All I need to tell the function is the name
that represents a label of that set of
of elements that we call an array.
That's exactly the same thing we're doing in code.
I just have different numbers.
So here I initialize an array list five
with the numbers five, seven, nine, 23 and 15.
I'm passing it to the two functions that were defined.
Just as a test, I am printing in line 28 and
line 32 the third element of the array.
I'm curious what happened when those functions
received the array in different ways
and they tried to print the same element.
What does it look like?
Is it the same?
Or is there a difference?
Let's run this code and we'll see.
First of all, there is no difference.
So, position three in the array, which is element four
because this is zero, one, two and three.
It's 23.
So, first of all, the answer is correct.
So the question is, how come those two functions
whose parameters were defined differently,
one was an array of int, the others a pointer
to an int, did exactly the same thing?
Even the printf statement is the same.
There's absolutely no difference in what the
functions do other than the definition of their parameter.
The only explanation is that int array in line 27
and int pointer are the same and exact thing.
This confirmed our earlier statement that
parameters, or functions, are nothing but pointers.