Hello, and welcome back. In this lecture, I want to provide a demonstration
for our previous challenge which was
understanding pass by reference,
having a parameter inside of a function take a pointer.
So let's create a new project. We'll call this one
pointer pass by reference because that's what it's demonstrating,
basic stuff here, new project,
console application c. Pretty straightforward here.
Let's just call this pointer underscore
pass by reference.
Again, demonstrating how we can use parameters inside of a function
to pass data and how that data will then be
modified outside the function.
So the first thing we're going to do is we're going to create our function declaration,
our function prototype. This is always
nice to do so that you can call anywhere in your program. We're just going to call it square.
Main thing in this function declaration is it's going to take a pointer.
So it's just going to take an end pointer, so we're just going to say int star
and then you can give it whatever name you want and you end it with the semicolon.
And our main function,
first we're going to do is we're just going to create a regular integer variable
and we'll just say int num and we'll just say hard code it to 9.
And what we're going to do is we're going to call our square function.
Our square function is not going to return any data, it's going to be void.
We have to pass it an address now though because our square function on line 4 is taking a pointer.
So we can't pass a num, we would have to actually pass in the address of num,
and then we would end it with a semicolon. So we call the function
using the function name and then passing the address of the variable.
Because the variable here in a line eight is not a pointer.
Then we can just basically print out the value of num.
And again, what you'll notice here is because we're passing in the address,
if we change the data in the function itself for that variable,
it's going to change outside of it. So here we're going to say the square
of the given number is
and it's just say percent d,
we'll add a line feed and we're just going to print out num,
and num is not going to be 9., I'll tell you that much.
And then we'll just return 0 for our regular function.
So now we have to actually implement the function, define it.
To define it, I'm just going to say void square.
We know it takes an integer pointer, so I'm going to say int star x.
And now we want to just multiply it by itself.
But we want to make sure that we don't change the address.
So what we could do here is we could make this a constant,
but you'd want to put the constant after the data type
because we don't want to change the value of what x is
or the address of what it's pointing to so we could say int constant.
If we didn't want to change the value of x,
we'd say constant. But we do want to change the value of x, we're going to square it by itself.
So to square it by itself, first off,
what we want to change is the ultimate value of x. So we have to
use the indirection operator. We have to de-reference it with the asterisk.
And then we're just going to say equals,
we don't want to do x times x because that's just going to multiply the addresses. X is a pointer,
we don't want to just get the value of x, we want to get the value of what x is pointing to.
So again, we have to de-reference it
and then we're going to multiply it by itself. We're going to de-reference it twice it.
Might be a good idea to put this in
parentheses just so it's very easily understood what's going on here.
You're de-referencing the pointer, getting the value of it and then multiplying
by its value and then you just add the semicolon, and that's it
that's all you have to do. So when we run this program, it's going to print out
81.
It's going to print out the value of num. Num is going to change inside square because
we're passing the address there.
We're not able to actually change though the pointer's value,
so we have the constant after the type.
So here we say build and it looks like
we didn't change the declaration.
And now we just want to make sure
that everything else is correct.
If you want to have the pointer
be constant, you have to put it after the asterisk.
So int start
constant means that you can't change
the value of x, which is the address of num.
If we put const before the int
or after the input before the asterisk,
it means that you can't change the value of what the pointer is pointing to.
So now when we run this we should see as output 81.
Again, what this tells us is
that we modified
the data. This variable,
this local variable num was modified inside the function and its value changed.
That's not normal. We have not learned that concept up until now.That's passed by reference.
But in c what's happening is you're passing in a copy of the address.
That's why it's really redundant, we don't even need the constant because
even if we were to change the pointer's value,
it's a copy of the pointer's value, so you don't really need to do that.
Now if we didn't want to use an existing number we could create a pointer
we just have to allocate memory for it.
So what we would have to do here is we could create an int star num.
But what we'd have to do is first we'd have to cast
the malloc because malloc comes back with a void pointer
but we can allocate memory dynamically.
And in here, we'd have to say
really just because we're creating one integer, we could just say the size of int
and that would allocate memory for us.
And then, all we'd have to do is pass into square num.
And then when we build this,
it would do the same thing but now
don't have a value for num either
so wouldn't actually know
what to what to compute, so you'd have to actually assign
to the the value of num we'll say here four,
you de-reference it. And then now when you build it and run it,
you should get 16.
Now the only thing here is we want to print out the value, so we want to say num.
And then when we run it, we should see 16.
And it's really just the same type of behavior,
and we do we see 16.
The only thing difference here was we were allocating memory
for what the point is pointing to as opposed to just pointing to an existing
memory location with ampersand.
Again, important things to note here, this number is changing
outside of the function. This is passed by reference
understand. Where we're using the de-reference operator, the indirection operator
to get the value of what the point is pointing to.
Understand that if the function takes a pointer
and we have a pointer, we can just send in the name.
If it's not taking a pointer
and it's actually a value,
then we have to de-reference it. And this is just like the scanf.
So here we'd have to say the ampersand.
And then also when we print it out,
we wouldn't have to de-reference anything because it's not a pointer
and then that would be the same thing here.
And then understand const two. Const before the int star
is saying that the value that the point is pointing to cannot change.
The const after the int start is saying that the address of the pointer can't, change the pointer's value.
I hope this was helpful.
Again, play around with this, change some syntax, mess around like I did.
It will help you learn a lot. Thank you.