void and void Pointer In C/C++

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

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

  • @venkateshmarni9834
    @venkateshmarni9834 6 лет назад +5

    Thank you CppNuts Team. Data about void* are crisp and clear.

  • @robindubey9670
    @robindubey9670 5 лет назад +2

    Addition:
    In c++ void fun(); and void fun(void); are same
    But in C
    Void fun(); means fun can be called with any no. Of arguments and void fun(void); means fun can be called only without argument.

  • @tayenderkumar3366
    @tayenderkumar3366 6 лет назад +2

    fun() and fun(void) are different in c, but same in c++
    In C fun() means any number of arguments, but in C++ fun() means no argument
    fun(void) means no argument in both C and C++

  • @ShadowaOsu
    @ShadowaOsu 5 лет назад +10

    There are 2 things I have to say.
    1) You CAN use const variables with void pointers. You just need to make the void* const as well. Meaning:
    const int* p = new int(20);
    const void* ptr = p;
    This will compile and work just fine.
    2) Your point about function pointer is right, but the example is wrong
    You tried to cast "fun" which is a *function* into type "void*". A function is not a function pointer and if you wanted to show that you should have created an actual function pointer like this:
    void (*f_ptr)() = &fun;
    And then use this in main like
    void* ptr = static_cast(f_ptr);
    This also won't compile but to show function pointers can't be casted into void pointers, this is an appropriate example.
    HOW EVER, it is possible to cast even function pointers into void pointers using reinterpret_cast but, quoting:
    "It should always work for every compiler, but it's not always safe"
    Meaning depending on your compiler it could be undefined behavior. So don't do it.
    But in case if anyone wants to know, this is how.
    auto f = &fun;
    void* ptr = reinterpret_cast(f);
    Edit:
    I know this isn't the topic of the video but because of the function pointer issue + using any raw pointer (void* in this case) is hard and unsafe, just use std::any. STL implementations are most likely best you can go with and since most of them are constexpr and template based, optimizer will optimize your code much more than you can by hard coding it.
    Plus, it handles everything automatically what you normally have to handle with pointers. And it even works with rvalues unlike void*.

    • @CppNuts
      @CppNuts  5 лет назад +4

      Thanks, it's really valuable comment and help someone seeking more info.
      I appreciate it.

    • @ShadowaOsu
      @ShadowaOsu 5 лет назад +3

      @@CppNuts Thank you! Also just realized I look like I was criticizing so much, I forgot to appreciate how good the video was. Thank you for the great content!

    • @CppNuts
      @CppNuts  5 лет назад +4

      Dude, it doesn't matter if you criticized but the important point is you are looking forward to help which is very important for me. And really i didn't feel you criticized at all.

    • @ShadowaOsu
      @ShadowaOsu 5 лет назад

      @@CppNuts I'm glad then :)

  • @srinu571
    @srinu571 6 лет назад

    casting of const pointer is possible with const void. we are not loosing constness after cast so possible.
    const int *p = new int(10);
    const void *vp = static_cast(p);

  • @rohitranjan4167
    @rohitranjan4167 4 года назад +1

    With void pointers is arithmetic can not be performed in a void pointer.
    Example:-
    void *ptr;
    int a;
    ptr=&a;
    ptr++; // This statement is invalid and will result in an error because 'ptr' is a void pointer variable.

  • @Skiskiski
    @Skiskiski 4 года назад

    Question: I do assign &var (7) to a *voidptr (void pointer). I do cast the void pointer to int pointer. Afterwards, I do print out *voidptr (gives me address of the variable) and **voidptr (gives me value of the variable). Why does not *voidptr dereference give me the value (7), but the address?

  • @richardtoth7240
    @richardtoth7240 4 года назад

    void pointer is basically HANDLE in the windows.h library. But why is a HANDLE useful? Why don't people just use a void* insteam of HANDLE?

  • @soulimanemammar2909
    @soulimanemammar2909 5 лет назад +1

    void *p =f where f is declared as a pointer to a function is possible

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

    Hello Sir, Can you please re-arrange Playlist for interview questions, most of the videos are repeating in playlist, which makes it difficult to keep track :) I follow your channel from last 1 year.. You are awesome !

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

      you find the same video in different playlist?
      or same video in same playlist?

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

      @@CppNuts same video in different playlist

  • @nikhilmane1621
    @nikhilmane1621 5 лет назад +1

    Can you plz tell which compiler you are using??

    • @CppNuts
      @CppNuts  5 лет назад +1

      I was using linux OS name xubuntu, code editor is sublime text editor, and the you need comiler which is basic comes in linux.

    • @nikhilmane1621
      @nikhilmane1621 5 лет назад +1

      Thank you for replying...Can you suggest any good compiler for windows OS.

    • @CppNuts
      @CppNuts  5 лет назад +2

      You can google like "how to setup sublime with mingw in windows" And you will get it, better search in youTube it self.

  • @KaramAbuGhalieh
    @KaramAbuGhalieh 4 года назад

    Do you have a github repo sir?

  • @aashishgoyal1436
    @aashishgoyal1436 6 лет назад +1

    Amazing. Thanks

  • @ShivMLinux
    @ShivMLinux 7 лет назад +4

    good job :)

    • @CppNuts
      @CppNuts  7 лет назад

      +Shiv Kumar Yadav Thanks dude.

    • @tusharmahawar4073
      @tusharmahawar4073 3 года назад

      @@CppNuts bro which IDE you are using In this video ?

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

      It must be Sublime text editor

    • @tusharmahawar4073
      @tusharmahawar4073 3 года назад

      @@CppNuts is it free or i have to purchased ?

    • @CppNuts
      @CppNuts  3 года назад

      Free

  • @sanjeevashoka7948
    @sanjeevashoka7948 4 года назад

    Thanks a lot sir...

    • @CppNuts
      @CppNuts  4 года назад

      So nice of you

    • @raghav5074
      @raghav5074 4 года назад

      Sir, Sanjeev is a noobra number one 😁😁😁

  • @subbumotepalli
    @subbumotepalli 5 лет назад +1

    Can anyone explain static_cast

    • @CppNuts
      @CppNuts  5 лет назад

      I have a video, search in RUclips.

    • @subbumotepalli
      @subbumotepalli 5 лет назад

      Thank u

    • @bludgeon1081
      @bludgeon1081 4 года назад

      Basically converts one type to another.
      For example if you can convert a int to a double.

  • @davemart92
    @davemart92 4 года назад

    how can I put this void pointer into a class?
    void * elimina(void * s){
    struct nodo *p,* aux;
    if(s==NULL)
    printf("
    pila vacia
    ");
    else{
    p=(struct nodo*)s;
    s=p->siguiente;
    free(p);
    }
    return(s);
    }
    Thanks!

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

    awesome

  • @OleguitoSwagbucks
    @OleguitoSwagbucks 4 года назад

    tanks dude

    • @CppNuts
      @CppNuts  4 года назад

      Thanks man!!

  • @agnesakne4409
    @agnesakne4409 5 лет назад +2

    ok

  • @mizginmeo.o194
    @mizginmeo.o194 5 лет назад +1

    ok :)