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; If nothing else, you seem to be a dereference away from having an integer. Soma...

  1. #16
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,370
    If nothing else, you seem to be a dereference away from having an integer.

    Soma

  2. #17
    quo
    quo is offline
    Registered User
    Join Date
    May 2011
    Posts
    116
    Quote Originally Posted by phantomotap View Post
    If nothing else, you seem to be a dereference away from having an integer.

    Soma
    what do you mean?

  3. #18
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    This is already the second thread about the same problem

    see compare void pointer

    You have never shown any code. If you really want a correct answer then it is time that you post a complete example.

    Kurt

  4. #19
    quo
    quo is offline
    Registered User
    Join Date
    May 2011
    Posts
    116
    Quote Originally Posted by ZuK View Post
    This is already the second thread about the same problem

    see compare void pointer

    You have never shown any code. If you really want a correct answer then it is time that you post a complete example.

    Kurt
    Code:
    void* pVoid;
    
    int val=7;
    pVoid = &val;
    
    getRecords("num",pVoid);//first call of function
    
    getRecords(NULL,"John");//second call of function
    
    
    getRecords(char* field,const void *value){
    
    if(strncmp(field,"num",3)==0)  { //this means value is int
       if(reinterpret_cast<int>(value)==24){
          cout<<"There was a match!"<<endl;
           counter++;
       }
    }
    else{//this means value is char*
     if(strncmp(value,"John",10)==0)  {
       cout<<"There was a match of type2!"<<endl;
    
    }}
    That's all I do,nowhere else in the code I need this void*.Just to check in the function getRecords if it's a char* or int and cout things.
    Is the casting from const void* to int correct?
    It returns large negative values

  5. #20
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Ok that might get us somewhere

    Code:
    void* pVoid;
    
    int val=7;
    pVoid = &val;
    
    getRecords("num",pVoid);//first call of function
    
    getRecords(NULL,"John");//second call of function
    
    
    getRecords(char* field,const void *value){
    
    if(strncmp(field,"num",3)==0)  { //this means value is int
       // here you should check for NULL otherwise strncmp() will segfault
       if(reinterpret_cast<int>(value)==24){
        // you are passing an int * that is cast to a void *
        // should be 
        //   if( * static_cast<const int*>(value)==24){
          cout<<"There was a match!"<<endl;
           counter++;
       }
    }
    else{//this means value is char*
     if(strncmp(value,"John",10)==0)  {
       // a cast from const void * to const char * is not implicit
       // you need 
       // if(strncmp(static_cast<const char *>(value),"John",10)==0)  {
       cout<<"There was a match of type2!"<<endl;
    
    }}
    Kurt
    Last edited by ZuK; 05-13-2012 at 07:32 AM.
    rags_to_riches and quo like this.

Page 2 of 2 FirstFirst 12
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