text
stringlengths
1
81
start
float64
0
10.1k
duration
float64
0
24.9
The asterisk that actually has special meaning in certain contexts, not just
1,968.69
3.78
multiplication.
1,972.47
0.94
But in this case, it specifies that the type of a variable
1,973.41
3.74
is not a char literally, but a char star, the address
1,977.15
3.79
of a char, a pointer to a char.
1,980.94
3.3
Now, why char?
1,984.24
0.7
I thought we were talking about strings.
1,984.94
1.666
But again, recall that a string is just a sequence of characters
1,986.606
3.174
back to back to back, and therefore, you can define a string
1,989.78
4.51
by the address of its first character.
1,994.29
2.8
Ergo, what we really need underneath the hood
1,997.09
2.31
is a data type that lets us store the address of a character.
1,999.4
5.29
There is no string.
2,004.69
1.68
And so what does this allow us to do?
2,006.37
1.92
Let me go into CS50 IDE, and let me declare then, on this line
2,008.29
4.01
here, that s, this time, will not be a "string" quote unquote.
2,012.3
4.98
That was from the CS50 library.
2,017.28
1.66
But rather it's going to be a char star.
2,018.94
3.022
It turns out that all this time get string, again, does not
2,021.962
2.458
return a string, it returns the address of a string, AKA
2,024.42
4.33
the address of the first character in a string.
2,028.75
3.65
And so the type of value it's returning is not just a number.
2,032.4
3.83
It's not just an int.
2,036.23
0.96
It's a special type of an int.
2,037.19
2.09
It's used for a different purpose.
2,039.28
1.51
It's simply an integer that represents the address of a char.
2,040.79
3.87
And the way you type out address of a char is literally char star.
2,044.66
4.74
So this, then, is identical to my previously in weeks past having typed
2,049.4
5.27
string s.
2,054.67
1.08
Now I'm going to start typing it as char star.
2,055.75
2.76
Meanwhile, t is going to be the same.
2,058.51
1.709
So when I prompt the user for another string there,
2,060.219
2.125
I'm going to store that return value inside of t.
2,062.344
3.006
And now, notice, just for good measure, I'm making sure
2,065.35
2.77
that both s and t are not null.
2,068.12
2.71
I'm using a bit of conditional logic there, saying if s is not null
2,070.83
3.04
and t is not null, it's safe to proceed.
2,073.87
2.099
Because recall that get string can accidentally return null sometimes
2,075.969
3.595
if your computer is out of memory, or something else goes wrong.
2,079.564
2.666
Or not so much accidentally, but by design.
2,082.23
3.17
But notice this new chunk of code.
2,085.4
2.85
It turns out-- and we know now from a moment
2,088.25
2.8
ago-- you can't just compare s against t.
2,091.05
1.822
They're not going to equal the same thing
2,092.872
1.708
if you type in two independent strings.
2,094.58
2.34
We need a special function that actually compares strings in a conceptual way.
2,096.92
5.33
I mean that a string is equal to another string if every one of its characters
2,102.25
5.83
equals every one of the other string's characters.
2,108.08
2.83
Thankfully, there exists in C a function that does exactly that called strcomp,
2,110.91
4.67
string compare, and it takes two arguments.
2,115.58
3.09
The first is a string.
2,118.67
1.14
The second is a string.
2,119.81
1.01
Or more properly, the first is the address of a string.
2,120.82
2.58
Or even more properly, the first is the address of a character.
2,123.4
4.3
The second is the address of a character.
2,127.7
2.25
And str compare is just going to hope that both of those strings
2,129.95
4.92
eventually end in a backslash zero, so that they
2,134.87
4.01
don't loop forever through memory.
2,138.88
1.6
They eventually hit that special null terminating byte.
2,140.48
2.85
And if so, and those characters are all entirely equal, you print same.
2,143.33
4.23
Else, as before, we print out different.
2,147.56
4.24
So now, when I compile this program, make compare 1,
2,151.8
3.64
and then I do ./compare1.
2,155.44
4.73
Now I'll type in david in all lowercase, david in all lowercase.
2,160.17
3.951
They're indeed the same.
2,164.121
0.999
Let's do it again.
2,165.12
0.94
Zamyla with a capital Z. Zamyla with a capital Z. There are indeed the same.
2,166.06
4.93
Let's do Zamyla with a capital Z. zamyla with a lowercase z.
2,170.99
3.97
Different.
2,174.96
1.09
And then FUBAR, clearly different.
2,176.05
2.95
Now we're actually comparing these things properly.
2,179
2.7
Because now we're appreciating what it actually means to be a string,
2,181.7
3.76
and we are underneath the hood comparing what we should be doing.
2,185.46
3.69
Now, underneath the hood, what is str compare doing?
2,189.15
2.21
Honestly, it's probably just a while loop or a for loop
2,191.36
2.71
that is iterating over the string and their lengths
2,194.07
2.67
and looking at the i-th character in each string,
2,196.74
3.05
and making sure they're all in fact equal.
2,199.79
2.49
Let's go ahead and fix copy with version 1 here.
2,202.28
2.58
Copy 1.c that I've written in advance now looks like this.
2,204.86
4.06
I still prompt the user for a string s with these lines here.
2,208.92
4.73
I then, just to be safe, say hey, wait a minute, if s equals
2,213.65
3
equals null, return 1.
2,216.65
1.179
And again, 1 is just arbitrary.
2,217.829
1.291
I just want to get out, lest I break something later.
2,219.12
3.59
Now, down here, this is a new line of code.
2,222.71
2.227
And this is perhaps one of the most powerful ingredients
2,224.937
2.333
we'll see this week, is this new function called malloc,
2,227.27
3.55
memory allocate.
2,230.82
1.64
This is a special function via which you can ask the operating system, Linux
2,232.46
5.05
in the case here, or Mac OS, or Windows, if you're running the code locally,
2,237.51
3.4
hey operating system, please give me a bunch of bytes of memory.
2,240.91
5.22
Now, why do I want this?
2,246.13
1.17
This program is copy 1.c.
2,247.3
1.64
The goal at hand is to create a program that copies a string s
2,248.94
4.24
and stores the copy in t, so to speak.
2,253.18
2.47
Last time, it was not sufficient just to say t equals s,
2,255.65
3.06
because that copied the addresses.
2,258.71
1.87
That didn't give me a copy of Z-A-M-Y-- Z-A-M-Y-L-A backlash 0.
2,260.58
7.31
It instead just gave me a copy of the address.
2,267.89
2.27