You honestly have a gift for teaching this stuff. Typecasting is an obtuse topic at times and a typecast pointer is very obtuse. I've never really understood them and how they are used, until now.
when it's great, it is always great and always will be. The pointers were my roadblock until now, but things have become smoother and I can follow without fear. Thanks, Kevin, for the great effort which is priceless.
holy crap not to be weird but im high and i was literally just wondering about void pointers and your video just came out so this is great!! thank you!! also your tutorials are so helpful. i greatly value the knowledge you impart.
Hahaha I had a weird feeling that "today, I should make a video on void pointers", maybe it was destiny! :-) You're welcome Anthony, I'm really glad to hear you enjoy the tutorials.
Awesome tutorial! 😃 To be even less ambiguous I always prefer to attach the star to the type, I mean: int* p = &a instead of int *p =&a. This way in my opinion it’s very clear that the variable type is “int pointer”. And this way also you make a clear difference between the way you declare a pointer variable and the way you deference a pointer variable. Which I often noticed leads to some initial confusion at the early stages of understanding pointers.
I was curious if you will mention that pointer arithmetic on void pointers is illegal according to ISO C. You did mention it! Excellent tutorial! Thank you so much! If we don't need a cast after malloc in C, can we use the following syntax to keep the instruction more generic? int* ptr = malloc(n * sizeof(*ptr)); (No need to adjust the right side of the assignment if the pointer type of "ptr" should change). Is this legal according to ISO C? Would you do it the same way professor? Thank you very much!
You're welcome! I'm not sure if that's legal according to ISO C, though it seems to compile OK in gcc at least and I don't see why it would be illegal. I don't know if I would do it that way or not, I think if I was changing the type of 'ptr' anyways, then it might not be a big deal to change the sizeof() as well. On the other hand it's nice to only have to change one thing, so I think it may depend on the situation.
@@PortfolioCourses I found it in the Standard (the latest one). Page 82 (Not the PDF page, but the page number that the document itself lists). 6. 5. 3. 4 The sizeof and alignof operators Paragraph 6. I would like to post the link, but then UA-cam deletes my post. There is also an example on "cppreference" in the chapter malloc.
@@starc0w Yes I have seen examples like this on "cppreference" and according to ChatGPT the sizeof operator does not need to dereference ptr to determine its size, it only needs its type.
Again, great video! I see the void pointer as a multi-type pointer with some restrictions. When I 'malloc' memory, I use the data type(s) to format the memory to the desired type in pedagogical way.
@@PortfolioCourses I think it's because you are an IT-professor and I'm absolutely not... :o) Your pedagical skills are also great and not many are capable of combining these two very diffrent tasks in a good way. I'm actually have dyslectic issues, but your very clear voice makes the learning much easier. Sorry for my English.
To me the whole thing with pointers devolves to simply memory access. A type pointer will read the memory address depending on the type(int* will read 4 bytes and will decode the bits as int). A void pointer is a typeless pointer, it holds a memory address but does not know what is there. I think the fact that all pointers(regardless of type) are of the same size was a big eye opener to me. That being said, when we are moving the void pointer via pointer arithmetic, then the movement acts like that of the char* where each move is done in 1 byte * n . A type pointer will move depending on it's type, like a int* will move 4 bytes. Thus recently I wondered if there is any benefit to a void pointer since it acts like a char*. Of course one of the benefits is that it's typeless and thus can be a pointer to any type. Making it more universal.
I did understand pretty well this thing about pointers... but I was wondering why the heck the function `qsort` you show in the video takes a pointer parameter which type is void*? `qsort` is gonna iterate over each contiguos address of the array, but how will it do that if the pointer type is void* which doesn't accept arithmetic operations?
You provide a function to qsort, and that function does the comparisons, typically using a typecast to convert from void * to something you can do comparisons with.
Is it fair to say that, when you “p is pointing to a memory address if any type of data” for void *p that we could also say: “p is pointing to a memory address that can hold a data type of any size”? If I am thinking of a memory address (size depending on the hardware if course) then it is a series of bits that are either high or low…and the data type tells us (in addition to helping write correct programs) how many bits we can store there? or am I taking my microcontroller class (in assembly) too seriously? 😅
I've never done serial communication before Ozkan, but thank you for the idea you never know maybe one day I can cover it, I'm looking into it now and it's interesting. :-)
Saw a video of something similiar to manipulate floats, i think it was indirection or something… like this: float a; uint32_t temp = *(uint32_t*)&a; then manipulate the bits on the float and cast (float*) on it again
Great question! :-) When we use a double pointer, and do pointer arithmetic, the compiler will adjust the pointer arithmetic to make it work for the size of a double (and this goes for any other type). With void pointers, this won't work. Typed pointers also allow the compiler to give us errors/warnings if we try to use them in ways that are unexpected.
You honestly have a gift for teaching this stuff. Typecasting is an obtuse topic at times and a typecast pointer is very obtuse. I've never really understood them and how they are used, until now.
Thanks so much for the kind feedback Brian, and I'm glad the video was able to help clear that up for you! :-)
when it's great, it is always great and always will be. The pointers were my roadblock until now, but things have become smoother and I can follow without fear. Thanks, Kevin, for the great effort which is priceless.
Another complex topic explained simply. Thank you for all your teachings. Your teaching style is top tier
You're very welcome Siya, I'm glad you enjoy the videos! :-)
holy crap not to be weird but im high and i was literally just wondering about void pointers and your video just came out so this is great!! thank you!!
also your tutorials are so helpful. i greatly value the knowledge you impart.
Hahaha I had a weird feeling that "today, I should make a video on void pointers", maybe it was destiny! :-) You're welcome Anthony, I'm really glad to hear you enjoy the tutorials.
😂 cheers
@@PortfolioCourses "Destiny is all " : Uhtred of Bebbanburg.
Awesome tutorial! 😃
To be even less ambiguous I always prefer to attach the star to the type, I mean:
int* p = &a
instead of
int *p =&a.
This way in my opinion it’s very clear that the variable type is “int pointer”. And this way also you make a clear difference between the way you declare a pointer variable and the way you deference a pointer variable. Which I often noticed leads to some initial confusion at the early stages of understanding pointers.
I found ur videos about c and u are an absolute genius in explaining things! Thank u so much for ur content!
Extensive explanation about this topic, Thanks Kevin.
the GOAT in c
I was curious if you will mention that pointer arithmetic on void pointers is illegal according to ISO C. You did mention it!
Excellent tutorial! Thank you so much!
If we don't need a cast after malloc in C, can we use the following syntax to keep the instruction more generic?
int* ptr = malloc(n * sizeof(*ptr));
(No need to adjust the right side of the assignment if the pointer type of "ptr" should change).
Is this legal according to ISO C?
Would you do it the same way professor?
Thank you very much!
You're welcome! I'm not sure if that's legal according to ISO C, though it seems to compile OK in gcc at least and I don't see why it would be illegal. I don't know if I would do it that way or not, I think if I was changing the type of 'ptr' anyways, then it might not be a big deal to change the sizeof() as well. On the other hand it's nice to only have to change one thing, so I think it may depend on the situation.
@@PortfolioCourses Ok! Thank you very much!👍
You're welcome! :-)
@@PortfolioCourses I found it in the Standard (the latest one).
Page 82 (Not the PDF page, but the page number that the document itself lists).
6. 5. 3. 4 The sizeof and alignof operators
Paragraph 6.
I would like to post the link, but then UA-cam deletes my post.
There is also an example on "cppreference" in the chapter malloc.
@@starc0w Yes I have seen examples like this on "cppreference" and according to ChatGPT the sizeof operator does not need to
dereference ptr to determine its size, it only needs its type.
Again, great video!
I see the void pointer as a multi-type pointer with some restrictions.
When I 'malloc' memory, I use the data type(s) to format the memory to the desired type in pedagogical way.
Thanks! :-) I always think of void pointers as something like "polymorphic behaviour" in C programming.
@@PortfolioCourses I think it's because you are an IT-professor and I'm absolutely not... :o)
Your pedagical skills are also great and not many are capable of combining these two very diffrent tasks in a good way.
I'm actually have dyslectic issues, but your very clear voice makes the learning much easier.
Sorry for my English.
If I saw *((char *)p) before, I'd definitely freak out! Thanks for your videos, this is no longer the case.
Me too and the difference between making a pointer and get the value from a pointer.
You're welcome, I'm glad the video helped with that! :-)
To me the whole thing with pointers devolves to simply memory access. A type pointer will read the memory address depending on the type(int* will read 4 bytes and will decode the bits as int). A void pointer is a typeless pointer, it holds a memory address but does not know what is there. I think the fact that all pointers(regardless of type) are of the same size was a big eye opener to me.
That being said, when we are moving the void pointer via pointer arithmetic, then the movement acts like that of the char* where each move is done in 1 byte * n . A type pointer will move depending on it's type, like a int* will move 4 bytes.
Thus recently I wondered if there is any benefit to a void pointer since it acts like a char*. Of course one of the benefits is that it's typeless and thus can be a pointer to any type. Making it more universal.
I agree that the size of all pointers being the same is a big eye opener for people as to 'what pointers are really doing'. :-)
Nice explanation, thank you Kevin.
I’m glad you enjoyed it Firas, and you’re welcome, I hope you are well! :-)
Thank you so much for great explanation
Would you mind making a playlist on all the pointer videos you've done so I can follow them chronologically?
Great stuff as always.
I'm glad you enjoyed it Arthur! :-)
nice explanation. thanks.
I did understand pretty well this thing about pointers... but I was wondering why the heck the function `qsort` you show in the video takes a pointer parameter which type is void*? `qsort` is gonna iterate over each contiguos address of the array, but how will it do that if the pointer type is void* which doesn't accept arithmetic operations?
You provide a function to qsort, and that function does the comparisons, typically using a typecast to convert from void * to something you can do comparisons with.
Is it fair to say that, when you “p is pointing to a memory address if any type of data” for void *p that we could also say: “p is pointing to a memory address that can hold a data type of any size”? If I am thinking of a memory address (size depending on the hardware if course) then it is a series of bits that are either high or low…and the data type tells us (in addition to helping write correct programs) how many bits we can store there? or am I taking my microcontroller class (in assembly) too seriously? 😅
Great video
Thank you, I’m glad you enjoyed it! :-)
hi can you make a video about serial communication in c?
I've never done serial communication before Ozkan, but thank you for the idea you never know maybe one day I can cover it, I'm looking into it now and it's interesting. :-)
@@PortfolioCourses 😁
please could you do an example using _Generic keyword??
Saw a video of something similiar to manipulate floats, i think it was indirection or something… like this:
float a;
uint32_t temp = *(uint32_t*)&a;
then manipulate the bits on the float and cast (float*) on it again
Cool! :-)
But it has nothing to do with void !!! 🙃
Nice
I'm glad you liked it! :-)
Dear Professor
Since you usually replied soon, I suspect you missed my message below. :-)
I haven’t had time yet to answer questions. :-)
awesome bro plz cont.....
So why not using only void pointers!? Any disadvantages in size of such pointer?
Great question! :-) When we use a double pointer, and do pointer arithmetic, the compiler will adjust the pointer arithmetic to make it work for the size of a double (and this goes for any other type). With void pointers, this won't work. Typed pointers also allow the compiler to give us errors/warnings if we try to use them in ways that are unexpected.