What are void pointers in C?

preview_player
Показать описание
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.
Рекомендации по теме
Комментарии
Автор

7:15 It's important to notice that bytes are in reveresed order at each element. It's called small endian. For example if there wouldn't be small endian arrangement, the first element, 16, should be represented as { 00 00 00 00 00 00 00 10 }. Firstly it's not very noticeable in the case if you used 16, 31, 32, 65 or other numbers that can be represented as a byte but if you exceed that byte, the small endian arrangement will be noticable. Ex. (unsigned) 256 would be { 01 00 } but with small endian representation it's { 00 01 }. So this program is only good for these small numbers.

nandorboda
Автор

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.

martingeorgiev
Автор

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

dracorep
Автор

honestly the way u explain pointers is very fascinating and easy to remember keep it up brother 😁

SafwanMashrafiSaraf-llwl
Автор

finally programming tutorials thtat can be watched on smartphone

gauravchaudhari
Автор

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 <stdio.h>

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:


shvideo
Автор

4:35 can cast it before dereferencing

arpit
Автор

void pointers are also used for assigning callback functions. That’s what I used void pointers for.

zuzukouzina-original
Автор

My brain is like splash-splash after this video

adeled
Автор

When you go to Walmart to buy some socks and end up with an ironing board, 2x HDMI cables, Fruity Pebbles, and foot lotion.

mercnem
Автор

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?

MrDp
Автор

How to calculate mean of all data type numbers using void pointer concept

jayaprakashms
Автор

Do you have a video where you discuss what hexadecimal is?

orjihvy
Автор

Stupid question: why print it in hexadecimal?

adeled