Search code examples
c++structcompiler-warningsvoid

invalid conversion from void* to struct* in C++


I have a problem related with this implementation :

struct arg_struct
{
    int low;
    int high;
}*args;

void segmentedSieve(void * arguments)
{
    arg_struct *args =  arguments;
 /****do something******/
}

I would like to compile the program in C++ because there are several variables that use C++ libraries in it...

the problem here lies on this line :

arg_struct *args =  arguments;

The compilation fails and compiler says that it's an invalid conversion from void* to struct*...do you know how to solve this issue?? any help is really appreciated..

here is the reproducible result, I use C++ for this :

struct arg_struct
{
    int low;
    int high;
}*args;

void request_from_someone(void * arguments)
{
    arg_struct *args =  arguments;
 /****do something******/
}

int main(){
/***nothing to see here, just trying to give a compilation error***/

return 0;
}

compilation error : enter image description here


Solution

  • In C++ you need to explicitly cast from void* to a real pointer type:

    arg_struct *args = (arg_struct *)arguments;