WEBVTT

00:01.020 --> 00:11.210
Talk about a similar concept to a raise in that is the concept called C strings C string is a kind of

00:11.210 --> 00:19.340
specialized character array with a very specific difference a C string will always have a known character

00:19.370 --> 00:26.130
at the end of the sequence that no character signifies the end of the string.

00:26.130 --> 00:28.520
So see an example.

00:28.520 --> 00:36.120
So let's say we have a narrator and calling this sir a string.

00:36.250 --> 00:38.470
I'll give it six characters.

00:39.420 --> 00:45.890
OK in going to set them all like this.

00:45.920 --> 00:50.930
So first one capital H.

00:52.730 --> 00:56.530
Next one he

01:00.350 --> 01:15.570
is.

01:15.770 --> 01:22.350
And the last character I'm going to set is actually going to be no character.

01:22.370 --> 01:26.650
So it looks like this

01:29.530 --> 01:33.060
is like Slash 0.

01:34.910 --> 01:41.830
So knows that we need one more character for the word.

01:41.990 --> 01:42.470
Right.

01:42.470 --> 01:48.210
So we always need one more than what how many letters there are in the word.

01:48.330 --> 01:56.270
So is only five characters right but we need six actually have it P A C string.

01:56.270 --> 02:00.900
Otherwise we didn't have this you'd just be a regular character array.

02:03.620 --> 02:07.190
So we can actually do this little bit differently.

02:07.430 --> 02:11.570
And let's say we had this.

02:11.920 --> 02:13.650
Sure.

02:15.160 --> 02:23.540
Another string say and say hello like that.

02:24.020 --> 02:26.820
So now we're using double quotes here.

02:27.080 --> 02:31.730
And also notice that we don't set a null character anyway.

02:32.000 --> 02:40.250
What's happening here is that the another string here points to the first first character.

02:40.580 --> 02:43.120
Each k.

02:43.190 --> 02:47.050
So this is actually pointing to this.

02:47.120 --> 02:52.340
Since this is a pointer this actually points to the very first character.

02:52.340 --> 02:59.670
And notice I also can't steer because we're not going to be able to change it after.

03:00.280 --> 03:03.830
And say we just see out our stuff.

03:03.820 --> 03:05.290
So let's see.

03:05.290 --> 03:06.590
Ouch.

03:06.850 --> 03:08.920
Our array.

03:09.040 --> 03:11.670
See what happens.

03:11.700 --> 03:13.100
Oh hello.

03:17.250 --> 03:26.660
In Whetsel SU output hours another string k.

03:26.770 --> 03:31.590
And also notice that the output Hello up here.

03:31.780 --> 03:35.610
It's not outputting the address like you think it should right.

03:35.810 --> 03:40.340
Like before this is outputting an address.

03:40.330 --> 03:49.410
So you do this can still pending address.

03:49.630 --> 03:59.800
And this is a very special part of C strings where it outputs really what you kind of want it to output.

03:59.810 --> 04:12.300
It doesn't just output the e address outputs the string itself and so here and this another string say

04:12.300 --> 04:13.530
I did this

04:16.690 --> 04:24.100
so that's how you know it's actually pointing to each k because we're actually taking the value pointed

04:24.100 --> 04:30.430
to by another string and it's not outputting the address like it normally would.

04:30.430 --> 04:34.160
It's outputting the actual character itself.

04:35.310 --> 04:41.470
In fact if you do this it just before you know puts the entire string.

04:41.550 --> 04:49.510
And so this is sort of a specialization for a scene out in the insertion operator here.

04:49.710 --> 04:55.140
It's specially per program so output string and not the address.

04:55.160 --> 05:03.320
So one of the reasons why we use this null character is for knowing when to stop when we use pointers

05:03.320 --> 05:05.900
to pass C strings to functions.

05:05.900 --> 05:18.800
So let's see an example of this scatter of all that and what say you had a function we'll call it string

05:18.800 --> 05:26.880
length in and give it a six string is a pointer and not a.

05:26.900 --> 05:28.100
An array here.

05:31.780 --> 05:35.030
It's fine it here.

05:40.890 --> 05:53.200
And an output the return back the link that the strings 8 only 0 starch and or use a different in a

05:53.540 --> 05:56.310
variable air call the

05:59.270 --> 06:14.560
set it to the start of our string and say if the string is not equal to the null pointer right you could

06:14.560 --> 06:20.200
technically pass in null pointer and this would still work fine.

06:20.740 --> 06:33.270
So if it's not we will go through the string using this other pointer and we'll check to see if we have

06:33.270 --> 06:37.320
hit the null character.

06:37.470 --> 06:45.810
OK so we're going to loop until we find that null character Amerson to say length plus plus and are

06:45.810 --> 06:54.810
also going to say P plus plus because we want to go to each character in the array or in this E-string

06:55.250 --> 07:02.450
way that and then all do is return.

07:05.100 --> 07:13.330
And here we will again do another initialization.

07:13.380 --> 07:19.170
So you also do for C strings you can also say something like this.

07:19.170 --> 07:23.870
So again notice we're not using the null character anywhere.

07:23.870 --> 07:31.890
We're just setting it to the string that we want and will figure out that it's actually six characters

07:31.890 --> 07:34.250
not five right.

07:34.380 --> 07:37.450
And C++ will kind of take care of that for you.

07:37.620 --> 07:39.440
For the initialization anyway.

07:41.040 --> 07:43.350
It's the link.

07:46.700 --> 07:52.530
C string Raemer this will output the actual string not the address or anything like that.

07:54.810 --> 07:55.430
It's

07:58.060 --> 08:01.970
called the function string.

08:06.860 --> 08:10.370
Like that.

08:10.960 --> 08:11.680
Run this

08:17.920 --> 08:27.060
from this.

08:27.290 --> 08:32.840
And the link below is five so it's not 6 right.

08:33.010 --> 08:35.620
So which is basically what you want.

08:35.620 --> 08:41.250
You never want to count the last null character because it's always there.

08:41.590 --> 08:45.350
And this had to be conc because this is Konst.

08:45.590 --> 08:57.270
They say you can't have you can't point to a constant pointer if your pointer is not icons as well.

08:57.610 --> 09:06.560
OK so no that we didn't pass the length of the C string here right.

09:06.590 --> 09:09.020
We're not passing any length fear.

09:09.470 --> 09:10.250
OK.

09:10.250 --> 09:19.250
So this really only applies to you see strings because all C strings have to end with this if they don't

09:19.640 --> 09:21.790
then it's just a character array.

09:21.950 --> 09:28.790
And often bad things will happen if you kind of think that it should have the null character but it

09:28.790 --> 09:30.530
doesn't.

09:30.920 --> 09:36.610
So this is really the major difference between C strings and character arrays.

09:36.650 --> 09:39.730
I always have this null the end.

09:39.870 --> 09:43.310
So in the next lecture we'll use practice.
