Talk about a similar concept to a raise in that is the concept called C strings C string is a kind of
specialized character array with a very specific difference a C string will always have a known character
at the end of the sequence that no character signifies the end of the string.
So see an example.
So let's say we have a narrator and calling this sir a string.
I'll give it six characters.
OK in going to set them all like this.
So first one capital H.
Next one he
is.
And the last character I'm going to set is actually going to be no character.
So it looks like this
is like Slash 0.
So knows that we need one more character for the word.
Right.
So we always need one more than what how many letters there are in the word.
So is only five characters right but we need six actually have it P A C string.
Otherwise we didn't have this you'd just be a regular character array.
So we can actually do this little bit differently.
And let's say we had this.
Sure.
Another string say and say hello like that.
So now we're using double quotes here.
And also notice that we don't set a null character anyway.
What's happening here is that the another string here points to the first first character.
Each k.
So this is actually pointing to this.
Since this is a pointer this actually points to the very first character.
And notice I also can't steer because we're not going to be able to change it after.
And say we just see out our stuff.
So let's see.
Ouch.
Our array.
See what happens.
Oh hello.
In Whetsel SU output hours another string k.
And also notice that the output Hello up here.
It's not outputting the address like you think it should right.
Like before this is outputting an address.
So you do this can still pending address.
And this is a very special part of C strings where it outputs really what you kind of want it to output.
It doesn't just output the e address outputs the string itself and so here and this another string say
I did this
so that's how you know it's actually pointing to each k because we're actually taking the value pointed
to by another string and it's not outputting the address like it normally would.
It's outputting the actual character itself.
In fact if you do this it just before you know puts the entire string.
And so this is sort of a specialization for a scene out in the insertion operator here.
It's specially per program so output string and not the address.
So one of the reasons why we use this null character is for knowing when to stop when we use pointers
to pass C strings to functions.
So let's see an example of this scatter of all that and what say you had a function we'll call it string
length in and give it a six string is a pointer and not a.
An array here.
It's fine it here.
And an output the return back the link that the strings 8 only 0 starch and or use a different in a
variable air call the
set it to the start of our string and say if the string is not equal to the null pointer right you could
technically pass in null pointer and this would still work fine.
So if it's not we will go through the string using this other pointer and we'll check to see if we have
hit the null character.
OK so we're going to loop until we find that null character Amerson to say length plus plus and are
also going to say P plus plus because we want to go to each character in the array or in this E-string
way that and then all do is return.
And here we will again do another initialization.
So you also do for C strings you can also say something like this.
So again notice we're not using the null character anywhere.
We're just setting it to the string that we want and will figure out that it's actually six characters
not five right.
And C++ will kind of take care of that for you.
For the initialization anyway.
It's the link.
C string Raemer this will output the actual string not the address or anything like that.
It's
called the function string.
Like that.
Run this
from this.
And the link below is five so it's not 6 right.
So which is basically what you want.
You never want to count the last null character because it's always there.
And this had to be conc because this is Konst.
They say you can't have you can't point to a constant pointer if your pointer is not icons as well.
OK so no that we didn't pass the length of the C string here right.
We're not passing any length fear.
OK.
So this really only applies to you see strings because all C strings have to end with this if they don't
then it's just a character array.
And often bad things will happen if you kind of think that it should have the null character but it
doesn't.
So this is really the major difference between C strings and character arrays.
I always have this null the end.
So in the next lecture we'll use practice.