So this time around, I want to look at something you may never have heard of before, if you come from
a language like and you've definitely never, never heard of, if you've never done any programming
before.
But it's the concept of pointers and pointers are incredibly useful and they are often a source of some
confusion to people.
But they're not that hurt, particularly Engo, which handles them really, really well.
So we have seen variables already like ver my strength and that is a string and I'm going to store value
in my string.
I call my string.
Has the value of green a color.
OK, so I'm not using it yet.
Still in error.
So let's just print that out so we don't have an error log print line.
My string is set to and then a comma and then the actual value, etc., which is just the variable itself,
no problem.
So if I run that, you know what's going to happen?
It's going to print.
My string is set to green with a capital G and it does.
OK, no surprises so far and nothing about Pointer so far.
Let's come down here and create a new function funk and I'm going to call it change using a pointer,
which is not a great name, but it's exactly what I'm going to do.
It will take one parameter which I'll call S, and it is a type string, but it's not an ordinary string.
It has an asterisk in front of it and it is a type of pointer to a string.
OK, so you can't see that because it's great out.
So let's actually do something here.
New value.
I'm going to create a new variable called New Value and I'm going to call it store that the value of
red with the capital R and I'm going to say my argument, which is s I'm going to say a star in front
of that.
So a pointer to S is given new value.
I've just decided read to it.
I could have just put red here, but I'm just making it light up.
And then after I print that line up here, let me just try to.
Put it like this, I'm going to call that function, I will call change using pointer and I will pass
in.
I can't pass in my mind variable called my string because that'll give me an error, my string.
And it says, oh, look at that, there's an error.
What's the error?
Will the areas that changes in pointer down here, it actually expects to have a pointer to a string.
And if I roll over this, it says cannot use my string type string as type string pointer, little asterisk
and means it's a pointer.
Well, what I need is a reference to that variable.
All a pointer is a pointer is it points to a specific location in memory and gives you a means of getting
that particular location in memory.
Well, as you probably guessed, when you create a variable, you're creating a variable in memory and
you're storing a value at a particular memory location.
So what I need to do is instead of actually passing a parameter as a as an actual variable to this function,
I want to pass a reference to that variable.
I want to point to the location in memory where that variables contents exists.
And I can do that by putting an ampersand in front of it.
Oops.
Get it in the right spot, put an ampersand in front of it and now it actually works.
So I'm going to do one more thing.
I'm going to copy this log print line and then I'll tell you why I'm going to all this trouble.
I'll say after.
Phone call, after I'm calling this function, my string is set to some value, so what do you think
is going to happen here, bearing in mind that this function we created returns nothing and that the
variable, my string up here exists only in the scope of the main function.
So according to what I told you about SCOP, I really should not be able to effect this variable, my
string down here.
And I'm actually not trying to address that variable by name down here.
And since this function returns, nothing, and I'm just calling it, I'm apparently not storing anything
back in here.
So what do you think might happen is to start the program, find the main function great, create a
variable called my string store the value GREENAN It printed out my string is set to green and then
I call this function that returns nothing.
And then I print that again after function called my string is set to.
You might think it would be green.
Obviously it's not or it wouldn't be spending all this time talking about it.
But look at what I am going to do.
First of all, let's run it.
So I'll run it.
Look at the output.
My string is set to green after function.
Call my string set to read.
So all I did in here was I took the pointer.
So here I said pass a reference to this variable, to this function.
Then this function says, OK, I'm now storing.
The value of that is nothing more than a reference to this variable in memory.
And if I come in here, you show this input logged print line and I say X is set to and I just print
s out.
What's it going to print?
Well, let's see what it does.
Go Roumain Go s is set to and then we have this hexadecimal number.
Well what the heck is that.
That is nothing more than pointing to a specific location on my computer in my computer's ram.
It's memory and saying X is set to whatever you find at this memory address.
So now that I have that memory address, I'm creating the new string called New Value, storing the
value of reading it.
I'm saying go to that memory address that you received up here and change its contents from whatever
they happen to be to this value.
So what I've effectively done is changed a locally scoped variable, a function, a variable that scope
to the main function.
I changed its value by passing a reference or a pointer to that variable, to some function.
So that's what pointers are.
So when you need to get the reference to a variable, you put an ampersand in front of it.
When you need to refer to an actual pointer, you put an asterisk in front of it.
Now, this will become very useful to us as time goes on.
I'm only introducing it to you now because you may never have heard of pointers before and you're going
to have to at some point in the not too distant future.
They're a little confusing at first, but the thing to remember is a reference to a variable.
Put an ampersand in front of it to actually create a pointer type, put it, put an asterisk in front
of it.
So ampersands for reference asterisks for the actual pointer itself when you're pointing to a location
in memory.
All right.
So just keep that in mind.
The best way to learn these things is to actually use them.
And when we start using this, it will make more sense if it doesn't make sense to you now.