Pointers - CS50 Shorts
Вставка
- Опубліковано 5 вер 2024
- ***
This is CS50, Harvard University's introduction to the intellectual enterprises of computer science and the art of programming.
***
HOW TO SUBSCRIBE
www.youtube.com...
HOW TO TAKE CS50
edX: cs50.edx.org/
Harvard Extension School: cs50.harvard.e...
Harvard Summer School: cs50.harvard.e...
OpenCourseWare: cs50.harvard.e...
HOW TO JOIN CS50 COMMUNITIES
Discord: / discord
Ed: cs50.harvard.e...
Facebook Group: / cs50
Faceboook Page: / cs50
GitHub: github.com/cs50
Gitter: gitter.im/cs50/x
Instagram: / cs50
LinkedIn Group: / 7437240
LinkedIn Page: / cs50
Reddit: / cs50
Quora: www.quora.com/...
Slack: cs50.edx.org/s...
Snapchat: / cs50
Twitter: / cs50
UA-cam: / cs50
HOW TO FOLLOW DAVID J. MALAN
Facebook: / dmalan
GitHub: github.com/dmalan
Instagram: / davidjmalan
LinkedIn: / malan
Quora: www.quora.com/...
Twitter: / davidjmalan
***
CS50 SHOP
cs50.harvardsh...
***
LICENSE
CC BY-NC-SA 4.0
Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International Public License
creativecommon...
David J. Malan
cs.harvard.edu...
malan@harvard.edu
Man I feel sorry for the people who might be skipping over these shorts. The main lectures are great but Doug just clears up so much for me here.
Agree. I am one of those people who skipped shorts and now I have to get back and watch them
@@bpospanov made the same mistake, catching up right now, and so many things have become much clearer for me ..
Bro, like literally no one has explained pointers in depth or as well like you have in this course. WHY are CS teachers not great at explaining this. It's like they expect us to hear their metaphors and completely understand how to code a pointer. I appreciate you going through all the logistical things that students get confused about, like why is there a asterisk on that value but not the other we, and why to we use ampersand sometimes. I think the teachers just assume we are either smart or dumb, but in reality we can only start learning once something makes sense to us logically, rationally, and most importantly if we know WHY we are doing this. I really wish my previous teachers would have just shown me this video.
Also pass by reference and values makes so much more sense to me now. Arrays are basically pointers but with no *, and the goal of using a * is to get a normal variable (char or int, whatever) to act like an array. Arrays are pass by reference and variables normally are not. The reason we might want this is because variables DON'T SAVE VALUES when we put them into functions (aka if variable x is put into a function where x = "hi", the end result will not save x as "hi" because this is pass by value). But array do get saved in functions. If we want to save values into variables just like how arrays automatically do (from inside a function), we use pointers.
IDK why I wrote this, I think just to vent and help myself understand this. I've been struggling on pointers for soooo long, thank you really, I appreciate it a ton.
Pro tip: Instead of declaring pointers as type* name, you can just declare them as type *name (there's no difference). This'll prevent any confusion that you might get from declaring multiple variables at once on the same line as stated at 23:49.
That's the question I was wondering throughout all time! Thanx for your tip, Neo!
(int* a = pointer) (*a = value stored on 'a' address) i think writing like this it's better for understanding because are different things
That's also what they doing in the CS50 lectures (at least in the 2019 version)
@@AlexandreMiguelf i agree, "unpack" the actual value in the memory while a is a data type, aka pointer, that stores reference(address), writing in this style is more intuitive
personally i like: type * name
This is amazing. Finally some C videos that are clear and understandable. Thanks! These are going to be lifesavers this semester.
Im a first year student of computer science at a local community college, just wanted to say thank you to the cs50 team, their tutorials and lectures are so much better and clearer than my school's. Got the aha moment immediately after this video, Lloyd and David are great at breaking down confusing topics and making them very comprehensible. Appreciate the help, wish you guys had all the CS classes at Harvard, but I guess thats asking too much.
brother has hopefully graduated
@@SaadKhan-kk4ni I hope so
@@rubiano3i hope so as well
For me, this is easily the best video on pointers on youtube. I wish I had this during my CS undergrad!
Two weeks worth of reading and lectures compressed into a few minutes that make 50 times more sense
27:13 the conclusion is probably the most useful part of the video
The notebook analogy was incredible! I’m not used to working with pointers but that part alone really cleared everything up. Keep up the awesome work!
It's been some years ago, but with headphones the "puff" sound from some midlow voice frequency could be diminished by putting a bit down around 500hz into the sound mix.
This video helped a lot. Doug you did great here.
Don't give up. Yesterday it seemed impossible to understand, today it's already much clearer.
Doug Lloyd you are the best! Thank you so. so. so. much. Can finally wrap my head around pointers!
OMG . wish I had a C language teacher like you 20 years ago !! thanks a million
The way how he explained pointers was really comprehensible. Thanks Dougg, best video so far :)
Beautifully explained Doug. Found your description and explanation so easy to follow.
Of course, it's going to take me a while to understand ti well enough to implement them into my work but that's on me!
Is my laptop dying or are there sometimes horizontal lines appearing onscreen?
same.. It's video fault
Pointers, a subject so heavy it makes youtube images start to flicker.
it's the green screen
same
there's a glitch in the matrix
Watching these shorts after the main lecture is very helpful
Doug my man, i want to hug you mate, thanks for the incredible explinations, so much make sense now... Keep the great work
Great video. I finally understand pointers. I also recommend others' in this comment section to search for "call by reference vs call by value" to really cement the concepts. Thank you!
The syntax distinction between the two uses of * in the context of pointers is very useful to me: int* c (declaration of pointer) and *c (dereferencing that pointer). The lecture isn't (always) always doing this, e.g. declaration of pointer p in lecture 4: int *p.
I was so confused right up until the very end in the final diagram where you showed the k and *pk, I finally understand how to use a pointer, thank you so much.
My man Doug dropping shorts like they're unskippable DLCs lmao
Thank you very much, David Malan and everyone on cs50 staff!! You´re amazing . All that for free is just unbelievable!
Holy cow I think I'm starting to understand pointers in C!!! Thank you CS50! :D
Thanks Mr. Lloyd, you are a good teacher!
Dereferencing was the part , I used to get stuck up . You just nailed it mate .Cheers
Good explanation! The only issue is "all pointers are just addresses". Actually pointers are variables which contain the address of another variable.
The best explanation of pointers I've seen or read so far (as a beginner).
Love what youre doing Doug.
Huge thanks!
I totally agree that what makes pointers so complex is its syntax. sometmes * is just literally a symbol represnetating it is a pointer when you declare it, and then next time, it's a dereference operator where it takes you to a value pointed by a pointer address
I really like when he says "it's a lot easier to think about it like that as a human being" XD
Awesome video but for a moment I thought my screen was broken with all the green lines 😂
"When we learn the difficult, everything else become easy". I mean, I think if we become experts in Pointers, so the entire C will become easy to learn and I guess that, all programming languages becomes easy too, like a domino effect. Awesome lesson Professor Doug Lloyd, thank you.
The moment that he explains that string is actually the collection of characters, but not a data type.. Oh man! I feel enlightened!!! :D
The last 5 mins of this video helped me a lot. I guess it's mainly because I am not a native English speaker. Most part of the speech just drifted in front of eyes.
I understand a pointer as a variable that holds the address of another variable. to extract the address of a variable, we use the & operator, which returns a pointer. derefencing is a way of getting the value located in memory. to achieve dereferencing we need the address of a varible.
arrays aren't just pointers. they are similar, but 1. they support different notation for accessing the memory they point to (no asterisk asterisk), and 2. you can't change the memory address an array variable holds, whereas you can change the address a pointer variable holds. when you pass an array to a function, the argument will be a pointer containing the same address the array points to, this is also referred to as an array "decaying into a pointer"
Finally, I'm here to understand more what pointer is.
Pointers are especially useful when you have an array of objects and you don't know which object stores this specific value and you wanna make a change to the object so you use pointers so you can actually change it.
What can I say? Absolutely clear and smooth explanation of pointers
Between the lecture and the short I’m surprised there hasn’t been a mention of developing a naming convention that makes it obvious if a variable is a pointer or an actual variable. Something like ending all pointer names with an underscore
kid: "mom, can i watch shorts from harvard cs50?"
mom: "no, we have shorts at home"
*shorts at home - 30 minutes
Really good job, Doug! Thank you
My mind exploded when he talked about 32 and 64-bit systems. Does that mean that the NES, an 8-bit system, can only store addresses (pointers) up to 0xFF?
I wish this video came out when i was in undergrad haha
Amazing video series! Absolutely clear and well formulated examples. Thank you so much for your time and effort you really do make the internet (and the world) a better place.
On a side note, I wish you would go more into the syntax like using pointers to structs and accessing the instance variables using -> etc.
thanks doug, once again a great summary to a abstract topic
I think it would be worth showing the "->" syntax for de-referencing struct properties.
This video has some really useful pointers.
@17:47 it was actually the Arrays Short where set_int and set_array were used; not Variables And Scope
I think you have phenomenal teaching skills. Bravo!
thank you Doug!
Thank you, Doug Lloyd!
This is the only pointer tutorial that actually explains there are 2 uses of *
Awesome video! Straight to the point, easy to follow, and full of useful info. Thanks!
At 17:76 when you refer to the example on Variables and Scope, actually it was the example at the end of Arrays video! ua-cam.com/video/YdSycMcxvY0/v-deo.html
also, these are virtual addresses. C programs work under the assumption they "own" the entire memory space, and the OS gives them a fake memory address starting from 0 to make it simpler for individual programs, but the OS may allocate bytes, even those of an array, all over the place, and the conversion of virtual to physical space is done by the OS or CPU under the hood.
thanks Doug
Simply amazing how good this video is.
BUT why do we use pointers at all. I mean why dont we just work with different variables at once but use pointer to point at an adress which in fact just takes the information stored in that adress its pointing at and stores in it self. Why dont we just use the Variable we point at in the first place. Thats the question I dont understand....
Thanks for this video; before this I was using pointers sometimes to get something working but the next time i use it, i know what i'm doing :)
If you take the context out of it, that neighbor's house part is very funny
finally i understood pointers so well . THANKS Lloyd
Love CS50! Very good explanations. Guys, you are the best))
I take that pointers work like global variables, since locally scoped variables cannot be changed outside of their scope. With a pointer you can access it and change it from anywhere just like you could do with a global variable ??
Amazing video, thank you so much!😍
I must _strongly_ object to the use of the 'T* p;' convention when declaring pointers at 23:28. It is objectively bad practice, it creates confusion both about declaration syntax and how expressions work in C, and it doesn't match the actual syntax of a declaration.
Yes, while the type of p is "pointer to int", there is no pointer to int _type specifier_ . Syntactically, the '*' operator is bound to the _declarator_ (*p), not the type specifier (int). The structure of the declarator is meant to match the structure of an expression of the same type in the code. If you have a plain integer variable named x, you access its value as
printf( "%d", x );
The type of the _expression_ 'x' is int, so it follows that the declaration of 'x' is written as
int x; // x is the declarator
If you have an _array_ of 'int' named 'a' and you want to access the value of the i'th element, you write
printf( "%d", a[i] );
The type of the _expression_ 'a[i]' is 'int', so it follows that the declaration of 'a' is written as
int a[N]; // *NOT* int[N] a; a[N] is the declarator
The int-ness of 'a' is given by the type specifier 'int', and the array-ness is given by the declarator 'a[N]'. The type "N-element array of int" is fully specified by the _combination_ of the type specifier and the declarator.
Now, if you have a pointer to an 'int' named 'p', and you want to access the integer value 'p' points to, you would write
printf( "%d", *p );
The _expression_ '*p' has type 'int', so it follows that its declaration is written as
int *p; // *p is the declarator
The int-ness of 'p' is given by the type specifer 'int', while the pointer-ness of 'p' is specified by the declarator '*p'. Just like with the array declaration above, the type "pointer to int" is fully specifed by the combination of the type specifier and the declarator.
We declare pointers as
T *p;
for the _exact same reason_ that we _don't_ declare arrays as
T[N] a;
In both a declaration and an expression the operand of '*' is 'p', _not 'T'_ , just like the operand of '[]' is 'a', not 'T'.
The reason you can write both
T *p;
and
T* p;
is that whitespace isn't significant beyond separating tokens of the same type. During the preprocessing stage the source text is broken up into a sequence of _tokens_ - identifiers, constants, punctuators, etc. If you write something like
inttest;
the preprocessor sees two tokens - the identifier 'inttest' followed by the puncuator ';'. It doesn't recognize 'int' as a separate keyword at this stage. You must use whitespace to separate 'int' from 'test':
int test;
The '*' character is a puncuator and can never be part of an identifier. You can write any of
T*p;
T* p;
T *p;
T * p ;
and the preprocessor will see 4 tokens - 'T', '*', 'p', and ';'. All of those declarations will be parsed as 'T (*p);'.
The 'T* p;' convention creates confusion. In the following declaration
T* p, q;
_only p_ is declared as a pointer to 'T'; 'q' is declared as an object of type 'T'. The declaration is always parsed as
T (*p), q;
but from the way the declaration is structured it looks like you intend both 'p' and 'q' to be pointers. It also doesn't work for pointers to arrays or pointers to functions
T (*aptr)[N];
T (*fptr)(void);
since the '*' operator must explicitly be bound to the declarator.
A _pox_ on Bjarne for popularizing the style with C++.
Here endeth the rant.
Is no one going to mention the little black dot flying back and forth on the screen starting around 15:12 ?
This videos awesome but somebody messed up the premiere export settings
thank u i under stand
When you say that "all pointers are just addresses", don't you really mean that all pointers are VARIABLES that hold an address? Otherwise it's too abstract. If I am understanding correctly, a pointer is not the address itself, but rather a memory location (represented by a *variable) that holds the "gps" coordinates(i.e. address) to a different memory location where the values (stored in binary) actually physically live in RAM world.
The way I've always thought about it is this. A pointer is just a variable. It would make no difference WHATSOEVER (if I'm not mistaken) if you say int p = &x , the star just tells the compiler that you INTEND to put some address inside that variable. Because at the end of the day it's just numbers, zeroes and ones, all a pointer is, is simply a variable that happens to hold zeroes and ones that represent an address in the memory rather than a value that you want to use directly
@@mohammedjawahri5726 Agree. It is just a variable that meant to be pointing an address in memory. But it is also stored in memory and it has its own address.
@@mohammedjawahri5726 Yeah, almost :) This here won't work "int p = &x" since the address of x isn't an integer. But in general I think you're right.
This is so much simpler and easier to understand(at least for me).
Great material, thank you.
@17:40 I don't see how the implication is that arrays names are just pointers. Like you made that leap.
Thanks a lot Doug, you've been really helpful.
Doug Lloyd for president of US&A!
amazing work keep on great CS50
amazing lecture, thanks a lot
lmao when ur uni prof doesnt explain anything, so u came to here !!! it was helpful thankyou
Thanks a lot!
Thank you! This is a life saver
Hey Doug, great video! Although, i had a doubt. Do the light green boxes (pointers) take memory and if we free(some pointer), do the light green boxes always remain in memory or what ?
I wish there were a few practice problems involving points in this video.
17:32 ain't arr[i] refer to the i+1 th element of the array? cause arr refers to the address of the first element, arr +1 to the second element and so on. correct me if I am wrong.
27:12 تمرين للفهم
I really should talk to my dean about hiring teachers like u, you are soo good in delivering the message
Pointers aren't real, they are just addresses, they can't hurt you. CS50 problem set:
thank you so much for this
very useful video!
Amazing explanation, thanks!
please please please do some video on the difference between char *arr[] and char (*arr)[]
great video
When you just finished watching a 2hr and 40 min video, just to watch another few hours more..
Oh god help me
So, if pointers are useful to know where the variable is allocated, how the computer actually knows where pointers precisely are?
The computer knows where a pointer is the same way it knows where any other variable is. A pointer is just a variable that stores the address of another variable. So, you can use the ampersand operator to see its address:
int x = 5; // initialize an int to 5
int* p = &x; // set a pointer to it
printf("%p", &p); // print the pointer's address using the %p format specifier for pointers
printf("%x", &p); // alternatively use the %x format specifier for hexadecimal numbers
Thank you sir
Pointers give us a different way to pass data between functions
I thought my graphic card drivers going crazy because of green screen lines appearing behind :D
This is the point that I gave up last year. Anyone else?
youuuuu doug Lloyd help many peoples in brazil, thanks!
what data item is also used to hold the address of a pointer? Also why can't strings be used to hold the address of other variables sine the hexadecimal notation is a string
pleas put dark back ground