What are void pointers in C?

  Рет қаралды 29,528

CodeVault

CodeVault

Күн бұрын

On paper, they seem like a weird concept... void pointer... as in... pointer to nothing? No, that's not it. In this video we'll explain what a void pointer actually is and how it can be used.

Пікірлер: 37
@SafwanMashrafiSaraf-ll1wl
@SafwanMashrafiSaraf-ll1wl 19 күн бұрын
honestly the way u explain pointers is very fascinating and easy to remember keep it up brother 😁
@gauravchaudhari618
@gauravchaudhari618 2 жыл бұрын
finally programming tutorials thtat can be watched on smartphone
@draco5991rep
@draco5991rep 5 жыл бұрын
Finally an accent which is understandable ^^' I really tried to follow all the indian dudes but sometimes I just wasn't sure I understand it correctly. They all made good videos (I think) but it was so hard to understand them, because they all try to beat eminem in saying the most english words in one minute xD
@warplanner8852
@warplanner8852 2 жыл бұрын
I find this young gentleman's accent (Russian?) very pleasant and his earnestness and energy in teaching most gratifying. While I understand the concept of void pointers, the only failing he had in this lesson was that the example wasn't exactly real world. Had it been so, then it would have been an excellent piece of instruction..
@shvideo1
@shvideo1 2 жыл бұрын
Thank you for this great video! Really enjoyed your presentation. As a novice C programmer, here's one (brute-force way) to specify to what type to cast the void pointer inside the function: #include void printBytes(void*, int size); int main(int argc, char *argv[]) { int arr[] = { 16, 31, 32, 65 }; printBytes(arr, 4, sizeof(int)); } void printBytes(void *arr, int size, int typeSize) { for (int i = 0; i < size; i++) { switch (typeSize) { case 4: // Cast the void pointer to appropriate type then dereference it to get it's value. printf("%08x", ((int*)arr)[i]); break; default: printf("????"); break; } printf("|"); } } Outputs: 00000010|0000001f|00000020|00000041|
@CodeVault
@CodeVault 2 жыл бұрын
Very nice! Good job!
@adeled8833
@adeled8833 4 жыл бұрын
My brain is like splash-splash after this video
@martingeorgiev999
@martingeorgiev999 Жыл бұрын
It's important to note that casting a pointer to a pointer of incompatible type is undefined behavior since it violates the C strict aliasing rule which is designed for code optimizations. You can however cast to and from void and char pointers and compatible pointer types. In particular 2:23 is erroneous (you can see that the compiler outputs a warning) but 8:04 technically isn't(the warning stays since the pointers are of different type but the code would always work fine and it isn't dangerous). GCC turns on this rule in -O2 and -O3 optimization levels with the -fstrict-aliasing flag.
@zuzukouzina-original
@zuzukouzina-original 5 ай бұрын
void pointers are also used for assigning callback functions. That’s what I used void pointers for.
@daniels4847
@daniels4847 3 жыл бұрын
Finally I understand it! Thank You!
@JC-jw2kw
@JC-jw2kw 3 жыл бұрын
Excellent video.
@reenko
@reenko Жыл бұрын
Love your hacker style so interesting
@matteopisati9966
@matteopisati9966 2 жыл бұрын
Well done !
@mercnem6855
@mercnem6855 3 жыл бұрын
When you go to Walmart to buy some socks and end up with an ironing board, 2x HDMI cables, Fruity Pebbles, and foot lotion.
@arpit5506
@arpit5506 4 жыл бұрын
4:35 can cast it before dereferencing
@user-or7ji5hv8y
@user-or7ji5hv8y 5 жыл бұрын
Do you have a video where you discuss what hexadecimal is?
@CodeVault
@CodeVault 5 жыл бұрын
No. I will release a video on it next week. Thanks for the question!
@AlanBarrettVideos
@AlanBarrettVideos 5 жыл бұрын
Man ascii
@MrDp9000
@MrDp9000 Жыл бұрын
But what if we cast the arr to lets say int or long int type? Then we dont need to put 4*sizeof() but only 4, right? Will it work?
@CodeVault
@CodeVault Жыл бұрын
Yes, but you would still need to iterate each byte of each int individually
@jayaprakashms6769
@jayaprakashms6769 2 жыл бұрын
How to calculate mean of all data type numbers using void pointer concept
@CodeVault
@CodeVault 2 жыл бұрын
You can't know the data type from just the void pointer. You'd have to have something else identifying the data type the void pointer points to
@xbz24
@xbz24 Жыл бұрын
I love you
@adeled8833
@adeled8833 4 жыл бұрын
Stupid question: why print it in hexadecimal?
@CodeVault
@CodeVault 4 жыл бұрын
For this example, we could've printed it all in decimal. It just looks better that way (every byte is 2 hexadecimal digits) and memory is much easier to read in hexadecimal (generally) than in decimal.
@adeled8833
@adeled8833 4 жыл бұрын
If we did it in just decimal(%02d), then it would all be the same, but the numbers would look normal ?
@adeled8833
@adeled8833 4 жыл бұрын
Im just having a hard time turning each element of an integer array into a char, since the format they are stored in the memory is different for char and int
@CodeVault
@CodeVault 4 жыл бұрын
We'd probably need to use %03d since numbers can go up to 255. What code do you have so far regarding that conversion issue? Can you post it?
@adeled8833
@adeled8833 4 жыл бұрын
CodeVault thank you for explaining! No, I don’t have one, I’m just trying to understand how the type change works
@gabehcuodsuoitneterp203
@gabehcuodsuoitneterp203 2 жыл бұрын
char* arr -> char *arr
@CodeVault
@CodeVault 2 жыл бұрын
I prefer to put the type information (and other attributes) in one place and declare each variable on a separate line. But declaring it like that is fine as well
Quirks when it comes to declaring pointers in C
4:31
CodeVault
Рет қаралды 4,2 М.
How to return and pass a struct to a function
13:30
CodeVault
Рет қаралды 30 М.
Iron Chin ✅ Isaih made this look too easy
00:13
Power Slap
Рет қаралды 36 МЛН
why do void* pointers even exist?
8:17
Low Level Learning
Рет қаралды 344 М.
Are Lists Evil? Creator Of C++ | Prime Reacts
14:30
ThePrimeTime
Рет қаралды 98 М.
Dereferencing in C
10:29
CodeVault
Рет қаралды 10 М.
The What, How, and Why of Void Pointers in C and C++?
13:12
Jacob Sorber
Рет қаралды 52 М.
What are double pointers in C?
14:43
CodeVault
Рет қаралды 42 М.
Arrays as function parameters in C
13:28
CodeVault
Рет қаралды 12 М.
you will never ask about pointers again after watching this video
8:03
Low Level Learning
Рет қаралды 2,1 МЛН
A const int is not a constant.
9:16
Jacob Sorber
Рет қаралды 66 М.
Essentials: Pointer Power! - Computerphile
20:00
Computerphile
Рет қаралды 463 М.
What are variadic functions (va_list) in C?
13:49
CodeVault
Рет қаралды 20 М.
Todos os modelos de smartphone
0:20
Spider Slack
Рет қаралды 65 МЛН
📱магазин техники в 2014 vs 2024
0:41
djetics
Рет қаралды 568 М.
iPhone 15 Pro в реальной жизни
24:07
HUDAKOV
Рет қаралды 487 М.
Samsung laughing on iPhone #techbyakram
0:12
Tech by Akram
Рет қаралды 6 МЛН