Question

I'm receiving an incompatible pointer type error warning although my code functions properly. First of all, here's a simplified version of my code:

typedef struct {
    int field1;
    int field2;
} my_struct;

void some_function(void **data){
    // do something with *data
}

int main(int argc, char *argv[]){
    my_struct *ptr = malloc(sizeof(my_struct));
    ptr->field1 = 5;
    ptr->field2 = 17;
    some_function(&ptr);
}

The my_struct type is one example, but I'm actually using multiple types, so some_func must accept void**, rather than my_struct**. Each time I call some_func in the manner above, I recieve the following warning, and yet my code functions properly.

warning: passing argument 1 of ‘my_func’ from incompatible pointer type

I'm unsure of why these types aren't compatible, and would like to remove the warning in some way. For answers, feel free to explain why this is happening and, if possible, how to remove the warning (cpp directive, maybe?). Thanks!

Was it helpful?

Solution 2

void* means (effectively) "a pointer to an untyped region of memory".

void** means (effectively) "a pointer to a region of memory, which contains pointers to untyped regions of memory".

Notice how the block in the middle has a type -- an array of void*. You're passing in effectively an array of mystruct*, which is a different type. So your compiler warns you.

Similar issues occur in C++ when trying to convert Derived** to Base**; but it applies here in C as well. (void* operates like the "base class" of all pointer types) Consider a piece of code like this:

void does_things(void** outParameter)
{
    *outParameter = malloc(42); // Looks fine; writing void* to void* :)
}

int main()
{
    my_struct* x = /*...*/;
    does_things(&myStruct); // Ut oh, tries to make x point to
                            // something that is *not* a my_struct
}

OTHER TIPS

Use a void * parameter type.

void ** is not a generic pointer type. void * is.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top