NULL Pointer | C Programming Tutorial

  Рет қаралды 10,680

Portfolio Courses

Portfolio Courses

Жыл бұрын

An explanation of the NULL pointer value in C, including common use cases of the NULL pointer value. Source code: github.com/portfoliocourses/c.... Check out www.portfoliocourses.com to build a portfolio that will impress employers!

Пікірлер: 27
@grimvian
@grimvian Жыл бұрын
Thanks for clearing the understanding of NULL pointers. I learned a lot, despite I have worked with pointers for some time. I am wondering about your use the command line is for educational purposes... I was positively surprised that you also covered a bit of using struct/nodes. A suggestion for pedagogical reasons at least for me and maybe others: If the struct and the related code can be seen, without scrolling, will make your explanation easier to understand. English is my second language, but I dare to suggest to rephrase the title to: "The versatile use of NULL pointers".
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Thanks for the suggestions. :-) I put the struct up at the top of the file to keep it global as is more normal. I try to keep the titles shorter because I've found the video performs better that way, and I try to put more details into the description (like in this case I referenced use cases in the description).
@grimvian
@grimvian Жыл бұрын
@@PortfolioCourses Thanks - I have only heard one other instructor who pronouce char as car - no issue :o) This is of course an educational situation. Is the tendency to have functions not bigger than a screen size and not wider 80 characters to have the code as clear and understandable as possible or?
@heitorheitorheitor8158
@heitorheitorheitor8158 Жыл бұрын
man I love your videos they helped me so much 😊
@PortfolioCourses
@PortfolioCourses Жыл бұрын
That's awesome that these videos are helping you! :-)
@ramakrishna4092
@ramakrishna4092 Жыл бұрын
Thanks
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Thank you Rama! :-)
@badrinarayanan
@badrinarayanan 3 ай бұрын
Thank you very much for these videos!
@PortfolioCourses
@PortfolioCourses 3 ай бұрын
WOW - thank you very much!!!!!! :-D
@idanmariani8601
@idanmariani8601 Жыл бұрын
love everything about your channel i also buy your linked list course super awsome.. :)
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Thank you for the kind feedback Idan, and I hope you enjoyed the course! :-)
@pwnwriter
@pwnwriter Жыл бұрын
Awesome vid as always
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I'm really glad to hear you think they're awesome, thank you! :-)
@vicsteiner
@vicsteiner 16 күн бұрын
Thanks again! Another very clear video. In my case (Ubunto + gcc) the realloc was returning a new address in both cases passing the malloc array or the NULL pointer. Not sure why. My doubt is, when realloc returns a new pointer do the old one gets freed automatically?
@ramakrishna4092
@ramakrishna4092 Жыл бұрын
Hi sir I have a doubt here when you free the pointer the memory before and after is same I noticed can you pls tell me why .... I have seen this video two times but I have problem with my understanding. Can you pls help me with that .
@PortfolioCourses
@PortfolioCourses Жыл бұрын
It's the same before and after calling free() because free() doesn't modify the memory address that the pointer stores. The function just doesn't do that. All free() does is "make the block of memory that was free'd available again". So when we use malloc() to allocate memory, there is some block of memory that is set aside to be used for our program. Calling free() will make this memory available again, it will no longer be set aside for our program. When we call malloc it returns the memory address for the block of memory that is allocated and we store that into a pointer variable. But free() does not actually modify the value that the pointer variable stores, it ONLY frees the memory AT that memory address. So that's why after calling free() the pointer is the same. And that's why it's a best practice to set a pointer variable to NULL after calling free if we intend to re-use that pointer variable again later, because we are making it clear to the rest of our program that the pointer "points to nothing" which helps prevent us from accidentally using it as if it were pointing to valid memory. :-)
@grimvian
@grimvian Жыл бұрын
In the video at 6:00, I'm wondering: if (ptr != NULL) { *ptr = 5; printf("*ptr: %d ", *ptr); // should free only be used here, because malloc only allocate , when ptr != NULL or? }
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You could do it that way, this video is just for demonstration and learning purposes. In general if we can't allocate memory correctly we would probably do something more than just output an error message, we might exit the program for example. In this video we're just exploring the NULL pointer value. 🙂
@Mnogojazyk
@Mnogojazyk Жыл бұрын
Can one make a substitute for free() so that it would free the pointer and then nullify the pointer? I have in mind something like void lose(void ptr) { free(ptr) ; ptr = NULL ; }
@PortfolioCourses
@PortfolioCourses Жыл бұрын
That's a great question and yes I think you could do that, but you would want to pass the pointer "by reference" aka "pass by pointer" in order to set the pointer to point to NULL. So something like this maybe... void my_free(int **pointer) { free(*pointer); *pointer = NULL; } .... int *mypointer = malloc(sizeof(int)); free(&mypointer); I'm sure it could be made more general with void but maybe I can make a video on this one day.
@Mnogojazyk
@Mnogojazyk Жыл бұрын
@@PortfolioCourses, I think a generalised procedure and a video would be great. Thanks!
@PortfolioCourses
@PortfolioCourses Жыл бұрын
@@Mnogojazyk Cool! 🙂
@KasualNoBrainer
@KasualNoBrainer 3 ай бұрын
i have to say it... bro is better than bro code
@PortfolioCourses
@PortfolioCourses 3 ай бұрын
Thank you for the kind feedback. :-)
@poisonmods
@poisonmods Жыл бұрын
Hummmm
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I hope you enjoyed it! :-)
@mohokhachai
@mohokhachai 6 ай бұрын
Out
Local vs. Global Variables | C Programming Tutorial
8:49
Portfolio Courses
Рет қаралды 3,4 М.
array vs &array Pointers Difference Explained | C Programming Tutorial
17:38
A teacher captured the cutest moment at the nursery #shorts
00:33
Fabiosa Stories
Рет қаралды 54 МЛН
ВОДА В СОЛО
00:20
⚡️КАН АНДРЕЙ⚡️
Рет қаралды 34 МЛН
Essentials: Pointer Power! - Computerphile
20:00
Computerphile
Рет қаралды 463 М.
why do void* pointers even exist?
8:17
Low Level Learning
Рет қаралды 344 М.
Null Terminated String Safety Issues | C Programming Tutorial
19:29
Portfolio Courses
Рет қаралды 3,5 М.
Introduction to Pointers | C Programming Tutorial
24:42
Portfolio Courses
Рет қаралды 107 М.
Not In 138 Years Have We Seen Chess Domination Like This
11:21
Epic Chess
Рет қаралды 33 М.
What exactly is NULL?
18:06
The Cherno
Рет қаралды 190 М.
Dynamic Memory Allocation | C Programming Tutorial
31:51
Portfolio Courses
Рет қаралды 80 М.
Premature Optimization
12:39
CodeAesthetic
Рет қаралды 778 М.
String In Char Array VS. Pointer To String Literal | C Programming Tutorial
9:58
you will never ask about pointers again after watching this video
8:03
Low Level Learning
Рет қаралды 2,1 МЛН