Pointers in C
HTML-код
- Опубликовано: 28 май 2018
- A pointer is a variable that holds the address of another variable. Used carelessly they are a sure way to write hard-to-understand programs. However, used correctly, they unleash the power of the C programming language.
Twitter: / garyexplains
Instagram: / garyexplains
I studied C native in University on Solaris and this explanation of pointers is far and away better.
Better on all fronts. Well done Sir.
This is just the basics, here he is only addressing it... wait until you work with dynamic memory and the heap... I've learned C long ago but I'm more familiar with C++. The basic way to create a block of memory in C++ is with new and new[] and to free them is with delete and delete [] respectively. Today, this is considered bad practice unless you truly know what you are doing and you have a very specific reason for using "raw pointers" as the conventional way is to use std::shared_ptr and std::unique_ptr... In C this is done with malloc() and free() and some other memory-based functions.
Me: Visual Studio for C with autocompletion for smallbrain
Him, a divine being: nano on a Rasperry Pi Zero for C.
@Christian Weissmuller Vim is evil, use emacs instead and enjoy your sprained pinky!
@@Victor-dt1uq Well Evil Emacs is kinda nice :D
@Christian Weissmuller vim is for noobs use xemacs!
Notepad++ gang.
you guys still use nano? real men just
echo "#include int main(){printf("Hello World");}" >> program.c
To people getting this error message from gcc: warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 2 has type ‘int *’
Change %x to %p and it will work.
yes, some things have changed with the new C standards in the compiler.
"%d" worked for me
Oh I wanted to make a tutorial on pointers myself called "How to SegFault in C"
Lol
I feel that despite not having any fancy visuals or elaborate analogies, I feel I intuitively understood more in these 10mins with your video than I did through multiple other longer videos combined. I don't know how you did it but thank you!
I have been browsing for tutorials and found many on RUclips for things I already know, I found yours to be one of the best and I will keep your link here with me. Job well done.
Currently in a college course learning C and this is the only explanation I've heard in class or on the internet that makes pointers make sense. Can't thank you enough.
My pleasure. I hope your courses go well 👍
I just started learning programming (several weeks now) and your video really helps me a lot in understanding pointers. Which is, from what I heard, is one of the hardest subject in C programming.
Great video! pointers are definitely one of the more tricky parts of C that I have faced, especially when going for data structures (linked lists, dynamic arrays... ) with allocating and freeing and so on...
4:30 & tells you the address, * tells you the dereferenced value
6:50 in c an array name is equivalent to its pointer
7:30 increment the pointer
sir please do more C tutorials
If there is enough interest then I will.
I'd love it. I'd even pay for it on something like udemy =.
+1
That would be amazing!
sir we are interested in programming tutorials do make more of them.
Excellent explanation. Simple, going straight to the point.
Your tutorial in C was superb ! Make more tutorials and gain the attraction of new developers and of course you are great !
I went through so many tutorials just to understand this...Only this single fast-paced video helped me understand... Thank you.
Please tell me there are more videos like this
I'm just in the beginning of getting into C after learning Python. There really is something beautiful about how basic and (relatively) low-level it is.
Scared the b'jesus out of me moving from the well ordered metropolis of Python to the wild west of C.
Thank you for demystifying pointers for me. They have been a real pain in my neck.
Now I understand how pointers work, after 4 years of trying. Thanks for explaining, Gary!
Dude, you're a godsend, I could not understand pointers for the life of me before watching this video
Been a while since you shared this, but a better explanation than my professor at uni explaining c++ pointers. Cheers.
One of the best video on pointers. Please make more such programming videos like Data Structures etc
Old guy here. I've been self-teaching Java and soon to start Android, but not sure how to approach learning Android. I decided to take a break and learn some C and explore Linux more in-depth which led me to this video. It was nice to see you using NANO and excited to hear about the Java/Android course. I'll have a look. Great job with this vid, short as it was, good introduction to pointers.
By far the best explanation of pointers I have come across
You literally taught me in 12 minutes! compared to what my professor taught us... Thank you!! also understood you much more better.
Thank you so much Sir .. Such crystal clear explanation with demonstration.
my teacher couldn't explain this properly. Thanks!
Lol
Your teacher tried to explain this? Mine didnt even bother to explain lol
Change your college
Haha same
Read books😔🙏🙄its not even a hard concept to grasp
I didn't learn anything new about pointers, but boy oh boy I had no clue some shells supported the exclamation point notation for repeating previous commands, that makes me excited.
Thank you! You explained pointers really well
Best pointers explanation I've seen!
please, do more classes like that!I am now subscribed.
Really helpful, thank you Sir!
thanks gary that helped a lot !
Thank you very much Gary. Very helpful.
concise, straight to point, thanks
brilliant! love how you made this video , thanks :)
tnx you explain in an interesting and clear way
The star (*) is called the indirect operator because it access indirectly the value of a variable through the pointer.
Thank you. We need more videos like this.
I will certainly do more programming videos including C and Python. However they aren't the most popular type of video, so I will only be able to do them from time to time.
Cool logo you have there , and thanks for making this video
Thank you Sir, subscribed in 2021, all the very very best from Romania :)
Thank you Gary!
Thanks a lot for the very good explanation. I am about to enter the final exam of C in college... I didn't understand the language well, but I may pass the exam... I think I need to study the C language again in detail..
please make a whole tutorial for C language.
Very helpful! Thank you
This was so incredibly helpful! Thank you!
Glad it was helpful!
Thank you for this informative video.
Thank you for the informative video. Is there any benefit to using a pointer to traverse an array vs the traditional way?
Old memories. Thanks. 😊
Very well done, thank you very much.
Great tutorial. Thanks
I already knew everything, but I still watched the whole video.
you have taught pointer better in 10 min than my professor has in 3 hours
That took me back more than 30 years..
love your contents here
can u make a video explaining the digital ciruit realization of how pointer works?
missed that part at school because of the covid , had trouble with the array part. Thanks for the video. Aannd subscribed
Great video Gary.
You're great! THANK YOU!!!
Very clear explanation of pointers
More c please!!! And c in embedded environment! Thank you Sir!!!!!
this is the best explanation ive come across so thank u for this. i am curious as to what the point of pointers are. to fill the array u also could have just referenced each index as well
If you had a large Image that you want to process in a function, should you pass the whole 5MB to the function or just a pointer? They are also useful for many types of data structures, see my video on linked lists.
very informative thank you
please do make a viddeo talking more about programming and python it really helps.
Straight from 90's!
The question is why should we prefer using pointer rather than using, for instance, the index number of array to loop it??
This is what I hoped this video would explain. Unfortunately I'm left feeling more confused than when I began
Pointers allow you to change the value of multiple variables from inside a function, and they even allow you to pass functions as arguments into other functions, both of which are impossible without them.
@@SamChaneyProductions thank you! As someone who is not familiar with C I couldn't quite understand why this was important.
There is a very interesting advantage but I really don't recommend it unless you really need the most performance.
When you use array indexing in a loop the compiler (in theory) generates code to bring into the CPU registers the base address of the array and the value of the index, then it must compute the address you want to get access to. So you need two read access and a sum (all this is in machine code so it is very architecture dependent).
When you use a pointer to access the elements of the array and you are traversing the array in a fixed pattern you can take advantage of pointer arithmetic, since you are incrementing the address by a fixed number the compiler generates code to bring the address value to the CPU register and the increment will be put into the ADD instruction code, so you save at least one instruction per access (no need to fetch the index value from memory).
For example, the code bellow shows the differences:
#include
int array[10]={100, 200, 300, 400, 500, 600, 700, 800, 900, 1000};
int main(){
//This is a bit slower but more clear
int val;
for (int i = 0; i
Better to use pointer when, for example, you don't know what the array size will be at the beginning. Let's take a text file which has to be parsed and values stored.
First, you do a malloc which gives you a pointer and begin to read the text file. Then, if you haven't allocate enough memory, you can do a realloc and continue to read and parse the file.
Great video! Thank you
Best explaination I have ever seen
Great video! Thank you.
super helpful, thanks!
this video made sense for me and it gave me an idea what a pointer really is to my small brain 😂
That sign behind you terrifies me, fully understanding what that will do. Happy debugging!
Why should that terrify you?
Here is a little C program using the code on the signpost!
#include
int main() {
int **ptr;
int *ptr2;
int x=42;
ptr = &ptr2;
*ptr = &x;
printf("x is %d
", **ptr);
return 0;
}
Gary mate you’re a legend
shouldn't printing the address of what pointer holds be with %p and proper type casting?
Sir, please do more videos on more C concepts.
Thanks a lot , can you please teach " C structures and unions" ?
It's easy 🙄🙄, I hope you have learned till now 🤣🤣
Your main function should return a value. Other declare it void. Good explanation! Very clear.
LOL, not the grammar police, but the code review police. 😂
Quick but not dirty :> well explained.
well done, really well done. Can you please continue to teach C in such a understandable way.
I am glad you liked it. I will certainly do more C tutorials from time to time, but my only concern is that they don't seem that popular.
i can see that from the views, I feel like there is so little interest in C because it is quite a difficult language to learn, especially as a first language. Its my first language I am learning but only because it's a module i have too finish for my degree . I would personally like to learn python first, love the language and the simplicity. I have learned a lot in this video and used it to do one of my tutorials. I've spent about 3 days now trying to figure out how to assign new values to an array and keep the memory addresses. Then it needs to give the new values for the array and the addressees. Just can't seem to get it right.....
The fact that the C videos are so unpopular might actually have to do with the fact that no-one is interested in learning it as a first language and because of the immense lack of useful videos on it, which is kind of a vicious cycle.
Big ups for Nano. Big ups for good display colors.
Salutations, friend.
Thx helped a lot
It dawned on me as you were typing (3:11) *aptr = &x because in a variable "aptr" you literally store the (hex)value of the address of an "x", which is nothing else than &x. No more, no less. The only thing that confuses me a bit is that doing "pointer arithmetics" decimal number is added to hexadecimal...
Decimal and hexadecimal are just ways we represent numbers for humans. The numbers are the same. Adding 15 to something and adding 0xF are the same thing.
has pointer exam in 2 days,
bless you RUclips algorithm, sweet child
I cant initialize in the middle of code I have to initialize any variable at in start? how can you do ?
I really appreciate ur effort
Thank you so much 😀
*GARY!!!*
*Good Morning Professor!*
Great video as always!
MARK!!!
There's so much more that can be covered with respect to pointers. Passing by reference, linked lists, dynamic allocation, struct pointers, function pointers, and using pointers with array syntax. Also, sizeof(ptr) vs sizeof(*ptr). Understanding pointers is essential to sophisticated C programming, and correct use of pointers is essential for robust and bug free code.
Yes, of course. This is an introduction video.
thank you very much
Dang this guy does a really good job
Absolute masterpiece
Great explanation
Glad it was helpful!
Very informative video 👌
Great video!
That was amazing thank you so much!
You're so welcome!
Hey Gary, can you do a video on the difference between C and C++ and C#? Thanks for these awesome videos!
Hmmm, that could be an interesting video... thanks for the idea.
@@GaryExplains ❤️❤️❤️❤️ I honestly can't believe you replied
Thanks a lot ! it was very clear.
In any case there are IDEs more friendly than nano to write code :)
The commandline rulez!
@@Tom-qz8xw LOL, with all due respect I will use whatever I like!
I'm getting some strange output. I'm getting 5 outputs. The first gives the value 2, then, values of the other array elements (all 0) then the fifth value is the memory location of the last element. Then, it repeats with 2, hen, 4, then zeros, until it prints the memory address of the last location. This happens 5 times until the array has been completely filled. It's weird ..I can't see what could lead to that behaviour,
Never mind..I had the second for loop enclosed inside the first..that was the problem. It's all good now.
I wish you where the teacher in my faculty 12 years ago....
I still can't explain pointers by myself.
I still can't understand where I need to put a pointer when I am doing an exercice.
Do you have some exercices to help me ?
Is the name of this video any reference to the old Amazon Books search Easter egg?
Can someone explain the purpose of a pointer, because you can just directly increment the index of the array (somenumbers) to update its values right?
Instead of using a pointer? Why not call the variable directly??
commenting four years late but thanks dude this was massively helpful
You're welcome!
Node* root;
What about constant pointers? My text goes through them far too fast and I don't fully understand lines like int const * const pointer = &adr;
When reading const, read from right to left instead of left to right. So, the pointer variable is a "constant pointer to a constant int". This means that after creating the pointer, you won't be able to change which object it points at, and you won't be able to modify the object it's currently pointing at. The first const (as in int const) means that the thing the pointer is pointing at can't be modified. The second const (as in const pointer) means you cannot change what object the pointer points to.
@@RushilKasetty thank you very much! Clear explanation