text
stringlengths
1
81
start
float64
0
10.1k
duration
float64
0
24.9
But if I for instance, call string s gets get
1,655.44
6.88
underscore string open paren close paren.
1,662.32
4.07
That again, is going to give me something like this.
1,666.39
2.26
And it's going to give me something like Z-A-M-Y-L-A backslash 0,
1,668.65
7.31
all of which I can think of, as before, like an array-- if sloppily drawn.
1,675.96
5.65
Maybe starting again at address 123.
1,681.61
2.36
And so that's what ends up in s.
1,683.97
2.085
But in my copy 0 program, recall that I did this.
1,686.055
3.315
String t gets s.
1,689.37
4.61
I didn't call get string again.
1,693.98
1.65
I just said, store s inside of t.
1,695.63
3.08
Effectively, I thought, making a copy of it.
1,698.71
2.99
What this left-hand side gives me is another box called t.
1,701.7
3.11
If this is s, this is t.
1,704.81
2.74
But what goes inside of this new box t?
1,707.55
3.32
Well, what goes inside is literally a copy of s.
1,710.87
2.34
What is s? s is 123.
1,713.21
1.94
OK. t then, is 123, which means later in my copy program,
1,715.15
6.55
when I simply decided to capitalize t bracket 0,
1,721.7
6.08
the first character in my string t-- that's
1,727.78
3.65
kind of misleading, because my string t is really just my string s.
1,731.43
4.65
They're sort of synonyms for one another at this point,
1,736.08
2.36
because one indeed equals the other.
1,738.44
2.02
And so what has happened is that I have gone to the first character in t,
1,740.46
5.9
which of course is z.
1,746.36
1.37
And recall from my example earlier when I typed it in all lowercase, David,
1,747.73
3.52
for instance, with a lowercase d, it capitalized it not only in t, but also
1,751.25
5.31
in s.
1,756.56
0.786
In other words-- and frankly, it doesn't really matter, typically,
1,757.346
4.184
what these addresses are.
1,761.53
1.24
I'm just using 123 and 234 because they're sort of easy to say.
1,762.77
3.98
But you can really think of s as again, being a map that
1,766.75
3.29
leads you to the string you care about.
1,770.04
2.62
A pointer, if you will.
1,772.66
1.15
Literally an arrow.
1,773.81
0.96
And t, similarly, can be thought of as a pointer.
1,774.77
3.17
And the key detail here is that because I've set s equal to t-- or t
1,777.94
6.5
equal to s-- they are effectively pointing at the same thing.
1,784.44
5.88
So strings are a lie.
1,790.32
1.33
There is no such thing as a string data type.
1,791.65
2.09
There are things called chars, characters
1,793.74
2.4
that can live somewhere in memory.
1,796.14
1.45
And we humans can arbitrarily decide that hey,
1,797.59
2.66
if we put a backslash 0 character at the end of a sequence of other characters,
1,800.25
4.74
we can all just agree universally to treat that as the end of a quote
1,804.99
4.03
unquote "string," that is a word or a phrase
1,809.02
2.27
or paragraph or anything even bigger.
1,811.29
2.3
But we need a convention for remembering where strings begin.
1,813.59
3.696
We've already solved the where do they end problem.
1,817.286
2.124
So where does the string begin?
1,819.41
1.32
It begins at an address.
1,820.73
1.83
It begins with a pointer.
1,822.56
2.21
And so this special data type that we declare in CS50's library
1,824.77
2.94
called string really is just in CS50 IDE an 8-byte value,
1,827.71
5.23
a 64-bit value that is just a really big number that represents
1,832.94
5.51
the address in memory of a string.
1,838.45
1.74
And I say really big just because the IDE gives you access
1,840.19
2.69
to lots of memory, certainly numbers bigger than 123.
1,842.88
3.14
But a string is just a number, is just an address, AKA a pointer.
1,846.02
6.43
And that explains, then, why all three of these examples
1,852.45
3.07
did not behave as I might have hoped, because rather
1,855.52
3.38
they were taking things a little too literally.
1,858.9
3.89
Or I was failing to appreciate what's actually going on.
1,862.79
3.62
Let's pause for a moment, take things down a notch.
1,866.41
2.49
Make things a little more real with a bit of claymation
1,868.9
2.49
that will motivate, eventually, peeling back this layer further
1,871.39
3.07
and seeing what's really going on.
1,874.46
3.21
[STRUMMED CHORD]
1,877.67
1.4
NICK PARLANTE: Hey, Binky.
1,879.437
1.083
Wake up.
1,880.52
0.74
It's time for pointer fun.
1,881.26
2.51
BINKY: What's that?
1,883.77
1.21
Learn about pointers?
1,884.98
1.748
Oh, goodie!
1,886.728
2.789
DAVID J. MALAN: Binky, who exist here only in claymation form,
1,889.517
2.583
is the product of a good friend of ours, Nick Parlante at Stanford,
1,892.1
3.372
who teaches computer science there.
1,895.472
1.458
You'll see more of Binky and hear more of Nick in just a moment.
1,896.93
3.62
But these here are sort of metaphorically
1,900.55
2.39
the training wheels that we've had on for the past few weeks.
1,902.94
2.56
And the goal now at hand is to take these off,
1,905.5
2.37
and to finally start looking at what's really going on underneath the hood.
1,907.87
4.8
And starting to remove, if you will, let's
1,912.67
2.426
see if-- [BANG] --probably not the best idea.
1,915.096
4.564
Remove, if you will, these training wheels,
1,919.66
2.02
and actually see what's going on, and understand
1,921.68
2.87
and take advantage of the same.
1,924.55
1.57
As follows.
1,926.12
0.78
Let's go ahead now into CS50 IDE, and go ahead and open up,
1,926.9
4.18
let's say, compare1.c, which I wrote in advance to look as follows.
1,931.08
6.41
And you'll notice that it works a little differently from version zero.
1,937.49
3.04
Here we have a prompt for string s.
1,940.53
2.26
And we store in it the return value of get string.
1,942.79
3.8
But notice what's on the left-hand side.
1,946.59
3.64
Char star s, all of a sudden.
1,950.23
2.58
Indeed, all of this time, I've been treating things
1,952.81
2.18
as though they are strings, literally.
1,954.99
3.54
But it turns out a string is just a synonym
1,958.53
5.11
for a data type known as a char star.
1,963.64
2.54
And the new syntax today, then, is this star operator.
1,966.18
2.51