Like Tree7Likes

const void* to void*

This is a discussion on const void* to void* within the C++ Programming forums, part of the General Programming Boards category; hi all! At some point of my C++ program I want to call a function which takes a void* argument ...

  1. #1
    quo
    quo is offline
    Registered User
    Join Date
    May 2011
    Posts
    116

    const void* to void*

    hi all!

    At some point of my C++ program I want to call a function which takes a void* argument and give a char*.I do as follows:

    Code:
    function(void* a);//function definition
    
    function("bloom"); //function call
    The error I get is:

    invalid conversion from 'const void*' to 'void*'

    How can I fix that?
    When it was an integer I just did this:

    Code:
    void* p;
    int value=3;
    p=&value;
    function(p);//function call
    but I don't know how to handle a char*.
    Any ideas?

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    If you know that function() will never modify the value that is pointed to by the void * then you should define it as
    Code:
    void function( const void * );
    in the first place.

    If function will modify the data then you cannot pass it a string literal.

    if for any reason you cannot change the signature of function ( maybe becaus it is used as a callback ) but it will never modify the data then just cast
    Code:
    function(reinterpret_cast<void*>("boom"));//function call
    Kurt
    Last edited by ZuK; 05-13-2012 at 04:19 AM.
    quo likes this.

  3. #3
    quo
    quo is offline
    Registered User
    Join Date
    May 2011
    Posts
    116
    Quote Originally Posted by ZuK View Post
    If you know that function() will never modify the value that is pointed to by the void * then you should define it as
    Code:
    void function( const void * );
    in the first place.

    If function will modify the data then you cannot pass it a string literal.

    if for any reason you cannot change the signature of function ( maybe becaus it is used as a callback ) but it will never modify the data then just cast
    Code:
    function(reinterpret_cast<void*>("boom"));//function call
    Kurt
    I will do the second as the only thing the function does is compare this to another string.
    Thanks a lot
    Inside the function how do I get that value?
    For example if the void* had an integer value as I mentioned above I just did:

    reinterpret_cast<int>(p);

    where p is the void*.
    How do I do this for a void* which has the char* value?

  4. #4
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    255
    Quote Originally Posted by ZuK View Post
    if for any reason you cannot change the signature of function ( maybe becaus it is used as a callback ) but it will never modify the data then just cast
    Code:
    function(reinterpret_cast<void*>("boom"));//function call
    reinterpret_cast is overkill for this scenario. The conversion from char* to void* happens implicitly. The problem is getting rid of the constness of the character literal. For this you want to use const_cast.

    Code:
    function( const_cast< char* >("boom") );//function call
    ZuK likes this.

  5. #5
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    If you understand what
    Code:
    reinterpret_cast<int>(p);
    does then you should also know how to cast to a char *
    Kurt

  6. #6
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    255
    Quote Originally Posted by quo View Post
    I will do the second as the only thing the function does is compare this to another string.
    Thanks a lot
    Inside the function how do I get that value?
    For example if the void* had an integer value as I mentioned above I just did:

    reinterpret_cast<int>(p);

    where p is the void*.
    How do I do this for a void* which has the char* value?

    So you have control over what the function does after all? In that case it would be much better to rewrite the function to take a const void* instead of a void*.

    To answer your second question:

    Code:
    void func( void* arg ) // <--- make this const unless there is a compelling reason not to!
    {
        char* text = static_cast < char* > ( arg );
        // ...
    }

  7. #7
    quo
    quo is offline
    Registered User
    Join Date
    May 2011
    Posts
    116
    Quote Originally Posted by antred View Post
    So you have control over what the function does after all? In that case it would be much better to rewrite the function to take a const void* instead of a void*.

    To answer your second question:

    Code:
    void func( void* arg ) // <--- make this const unless there is a compelling reason not to!
    {
        char* text = static_cast < char* > ( arg );
        // ...
    }

    Yes, I did change the void func(void* arg ) to const void*
    but then it complained about

    Code:
    reinterpret_cast<void*>("bloom")
    so I changed it to

    Code:
    reinterpret_cast<const void*>("bloom")
    Ok I'll do what you told me

  8. #8
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    255
    You're still using reinterpret_cast though, which is not necessary in this case. You may want to read up on what the 4 cast operators provided by C++ are for. ---> C++ Casting: What are the C++ casting operators? - CodeGuru Forums

    Also, beware that using void-Pointers should generally be a last resort in C++ because any time you casting anything to a void-Pointer, you're essentially chucking type safety out the window.

  9. #9
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652
    You ignored or failed to understand the first answer from Zuk, which was:

    If you know that function() will never modify the value that is pointed to by the void * then you should define it as
    Code:
    void function( const void * );
    in the first place.
    When you say
    I will do the second as the only thing the function does is compare this to another string.
    then obviously the argument is not going to be changed within the function, so the function argument should definitely be const.
    antred likes this.

  10. #10
    quo
    quo is offline
    Registered User
    Join Date
    May 2011
    Posts
    116
    Quote Originally Posted by rags_to_riches View Post
    You ignored or failed to understand the first answer from Zuk, which was:



    When you say

    then obviously the argument is not going to be changed within the function, so the function argument should definitely be const.

    Yes I got it now,by changing the function argument to const void*
    there was no need for casting.
    But for the opposite, having a const void* and want to cast it to char* is the casting necessary?

  11. #11
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Why do you want to convert to a char * if you only want to compare ?
    Kurt

  12. #12
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    255
    Quote Originally Posted by quo View Post
    But for the opposite, having a const void* and want to cast it to char* is the casting necessary?
    Casting from a typed pointer to a pointer to void is always implicit.

    Casting from a
    pointer to void to a typed pointer always requires a static_cast. On top of that, you may have to use const_cast if the source is constant but the target isn't (but this should ring alarm bells ... casting constness away is almost never a good idea).

  13. #13
    quo
    quo is offline
    Registered User
    Join Date
    May 2011
    Posts
    116
    Quote Originally Posted by ZuK View Post
    Why do you want to convert to a char * if you only want to compare ?
    Kurt
    I thought I had to...
    So I can compare a const void* a with a char* name simply by doing this?

    Code:
    if(strncmp(a,name,15)==0)
    //do thtings

  14. #14
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    int strncmp(const char *s1, const char *s2, size_t n);
    You see strncmp() it wants const char *

    you compare
    Code:
    if ( strncmp(static_cast<const char*>(p), "whatever", 8 ) == 0 )
    Kurt
    Last edited by ZuK; 05-13-2012 at 06:38 AM.

  15. #15
    quo
    quo is offline
    Registered User
    Join Date
    May 2011
    Posts
    116
    Actually I just found out that since I changed my function argument from void* to const void*
    when I want to cast my void* value to an integer it doesn't work.
    That's what I did:

    Code:
    const void* p;
    int val=15;
    p=&val;
    
    function(p); //function takes argument void*
    Inside the function:

    Code:
    function(const void* p){
      
       int vvv=reinterpret_cast<int>(value);
    
    }
    why isn't that working ?
    Last edited by quo; 05-13-2012 at 06:48 AM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error: invalid conversion from 'const void*' to 'void*'
    By Wahidin Wahid in forum C++ Programming
    Replies: 10
    Last Post: 04-17-2012, 03:17 AM
  2. error invalid conversion from ‘const void*’ to ‘void*’
    By Wahidin Wahid in forum C Programming
    Replies: 3
    Last Post: 03-27-2012, 09:18 PM
  3. Replies: 12
    Last Post: 03-27-2009, 03:36 PM
  4. Invalid conversion from 'const void*' to 'void*' error
    By prawntoast in forum C Programming
    Replies: 3
    Last Post: 05-01-2005, 11:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21