Thread: const void* to void*

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

    Soma

  2. #17
    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
    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
    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
    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 06:32 AM.

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, 02: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, 08:18 PM
  3. Replies: 12
    Last Post: 03-27-2009, 02:36 PM
  4. Invalid conversion from 'const void*' to 'void*' error
    By prawntoast in forum C Programming
    Replies: 3
    Last Post: 05-01-2005, 10:30 AM