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
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..
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|
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.
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.
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
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
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
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..
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|
Very nice! Good job!
honestly the way u explain pointers is very fascinating and easy to remember keep it up brother 😁
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.
finally programming tutorials thtat can be watched on smartphone
My brain is like splash-splash after this video
Yeah XD
4:35 can cast it before dereferencing
Love your hacker style so interesting
void pointers are also used for assigning callback functions. That’s what I used void pointers for.
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?
Yes, but you would still need to iterate each byte of each int individually
When you go to Walmart to buy some socks and end up with an ironing board, 2x HDMI cables, Fruity Pebbles, and foot lotion.
Do you have a video where you discuss what hexadecimal is?
No. I will release a video on it next week. Thanks for the question!
Man ascii
Finally I understand it! Thank You!
How to calculate mean of all data type numbers using void pointer concept
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
Excellent video.
Well done !
Stupid question: why print it in hexadecimal?
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.
If we did it in just decimal(%02d), then it would all be the same, but the numbers would look normal ?
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
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?
CodeVault thank you for explaining! No, I don’t have one, I’m just trying to understand how the type change works
I love you
char* arr -> char *arr
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