So up until this video we have done mostly theory but I needed to do that to bring you up to speed guys.
So now what we're going to do is we're going to make a program that takes someone's name and age and
outputs it to them says a pretty simple program.
So the first thing we need we need to make some room make some space.
So let's define a name array that takes 20 bytes.
So this will take the full name and it will store it into this name array and what we're gonna do is
we're gonna go scan f percent tests which means we are wanting to read a string and then we'll just
provide name.
So while this will do is scan f reads from the keyboard right.
Percent S means we're reading a string and then name is the variable we want to actually input that
data into.
Now what happens when you put a name here is it will take the address the memory address of array and
it'll pass it to the Skynet function.
That's what's happening here and you can also pass addresses by putting in an ampersand by there.
But for arrays without the ampersand automatically does that for normal variables on arrays you have
to use the ampersand here guys so down here then I want you to go print f percent s and just before
that go Your name is a sentence and in the next argument we just pass the name and also do a new line
as well at the end.
So this should be pretty straightforward to understand.
We read in the name using scan if the system knows we want to read in a string because we use percent
tests and the string is basically a series of characters.
So scan F will refill the keyboard and a will anything we input it will store into our name array and
then we output.
Your name is.
Percent test and we pass in the name.
So it will give us something to us.
So you go back to the terminal that makes you seeded into your directory and we're gonna go up twice
enter and then we're gonna go main enter.
And now nothing happens because this is what is waiting for us to enter our name so I'm just gonna do
Dan press enter and it says your name is done.
Not bad not bad at all.
So what we can do here then is we can say uh we can now read and then their age.
So just up here if you go in your age equals zero and you do scan f percent ie for integer and then
we do age and this won't work.
Make sure there's an ampersand here guys.
So you don't run into problems because we're getting the address of the age variable I'm passing it
the scan half.
So what happens if all our variables have memory addresses.
So by using Ampersand we're passing the address to scan F. So.
So scan F. knows where to store on number that we enter.
So then it says.
So then down here do another print f your age is percent high.
And then in the second argument passing age and again use an ampersand here guys because we want to
read in the address go back to the terminal run your program to main again.
Now enter your name enter your age
Oh yeah.
In this case we shouldn't use the ampersand because we are out putting a literal value whereas here
we need to use the ampersand because scan F. expects somewhere to store it here.
We don't need to know the address.
We just need to know the value so that was my mistake.
Go back in the parlor again and run main enter your name then your name is done your age is twenty four
so then you know if you want to be cool about it you can say and in ten years you will be percentile.
And then here you can go age plus 10.
So now if we do that again and we run main then your age is 30 and in ten years you'll be 40 so yeah
pretty simple stuff.
So just to demonstrate this then if we want to provide the ampersand then we try to compile lab
compiles fine but if we run it now type then and type your type your age.
What's this.
What's happened.
It crashed.
The program crashed on this because you need the ampersand because if we just provide age it's the equivalent
to passing in this zero.
So he's saying to the system because age is equal to zero.
Right.
So it's the equivalent of saying I'm reading an age and still story and address zero but address zero
is not age favorable and that's the problem right.
So you need to provide the ampersand and I'll explain more in the next lecture about memory addresses.
Okay guys this some other stuff I want to just go over quickly so you notice we provide an ampersand
age but not for name.
The reason for this is because arrays you don't have to provide the ampersand if you just provide the
the actual variable name of an array it will automatically take the Empress a lot more.
I'll take the address automatically.
Whereas for normal variables the ampersand is required because otherwise it will take its normal value.
So that clears that up.
Okay.
Secondly thing I want to second thing I want to discuss is if you run the program again and you take
full name
you will see you just recompile that a minute.
You will see that it doesn't work.
It just says your name is Paul.
The reason for that is because scan f stops at the nearest whitespace.
In other words a space or a no.
It's something that's a whitespace right.
So there's another issue with this code.
We don't actually check if the data we're reading fits in the bounds of the array.
Remember what I was saying earlier that is very important.
So because of that it means that we can enter a really large name that's above 20 bytes and it might
not even crash the program doesn't always does but that doesn't mean it's right if it doesn't crash
the program.
Chances are you've corrupted some data but it has led to a crash.
I just do that and you can see a crash the program that time now because we've exceeded the 20 bytes
we stored this which is way more than 20 bytes.
So what's happened is it's gone over the 20 bytes it's probably overall store age variable or maybe
these ones depending on the order that the compiler compiled them into the binary file.
And because it's overrode those values it's actually led to a crash because we've probably overwritten
the return address or something.
Now remember I was saying hackers can exploit this to nasty things so I'm gonna show you in one line
of code how to fix first this problem we're only allowed to enter a first name because it sees the space
and stops and secondly the second problem where it overflows the buffet.
So we're going to fix all of that one line of code.
Now the function to use to do this is called F gets f gets read from a file.
Now I know what you're thinking well this isn't a file right.
Well in C you have what's known as STV In an STV outwards.
Now both of these things are file pointers.
They can be used as if they were a file on disk.
Even though they're not so STV and contains any input that we pass through our program while it's running.
STV out contains any data we're outputting OK so that makes sense.
Now let's get rid of our scan f name here and we're gonna do f gets as to ask for a buffer as this passing
name.
And remember you don't have to pass an ampersand here because arrays they automatically put the address
when you reference them by name.
Whereas normal variables you need to provide the ampersand to say this is an address.
Now we go here.
It asks for a max counter suggest you size of name which will be 20 bytes.
Right.
So size of gets the size of the name variable which is 20 bytes asked for a file stream just type STV
in and now if we compile that now and we run that you can say Paul O'Brien age 90 and it will say your
name is Paul O'Brien your age is 90 and in 10 years you'll be 100.
Now type loads is rubbish again let's overflow the buffer what happens.
It doesn't work.
It stops us.
It only allows it only allows us to enter a name of twenty bytes so it stops it stops putting more data
into the array.
When we reach that 20 byte cap so I hope that sums up a lot of this stuff for you guys.