What are void pointers in C?

Поделиться
HTML-код
  • Опубликовано: 6 ноя 2024

Комментарии • 38

  • @draco5991rep
    @draco5991rep 5 лет назад +35

    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 3 года назад +3

    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  3 года назад

      Very nice! Good job!

  • @SafwanMashrafiSaraf-ll1wl
    @SafwanMashrafiSaraf-ll1wl 3 месяца назад

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

  • @martingeorgiev999
    @martingeorgiev999 Год назад +1

    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.

  • @gauravchaudhari618
    @gauravchaudhari618 3 года назад +2

    finally programming tutorials thtat can be watched on smartphone

  • @adeled8833
    @adeled8833 4 года назад +4

    My brain is like splash-splash after this video

  • @arpit5506
    @arpit5506 4 года назад +2

    4:35 can cast it before dereferencing

  • @reenko
    @reenko 2 года назад

    Love your hacker style so interesting

  • @zuzukouzina-original
    @zuzukouzina-original 8 месяцев назад

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

  • @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  Год назад +1

      Yes, but you would still need to iterate each byte of each int individually

  • @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.

  • @user-or7ji5hv8y
    @user-or7ji5hv8y 5 лет назад +1

    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

  • @daniels4847
    @daniels4847 3 года назад +1

    Finally I understand it! Thank You!

  • @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

  • @JC-jw2kw
    @JC-jw2kw 3 года назад

    Excellent video.

  • @matteopisati9966
    @matteopisati9966 2 года назад

    Well done !

  • @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

  • @xbz24
    @xbz24 Год назад

    I love you

  • @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