Ignore warnings "initialization from incompatible pointer type"?

Is there a compiler directive to ignore the warnings “initialization from incompatible pointer type” in Hardware_MouseDrivers_GPM_Methods and Hardware_MouseDrivers_DevInput_Methods ? Disabling alerts around the world is not an option.

 #include <stdio.h> /* Mouse driver interface */ typedef struct _Hardware_MouseDriver { int (*open)(void*, char *); int (*close)(void*); int (*poll)(void*); } Hardware_MouseDriver; /* GPM */ typedef struct _Hardware_MouseDrivers_GPM { char *path; } Hardware_MouseDrivers_GPM; static int Hardware_MouseDrivers_GPM_Open(Hardware_MouseDrivers_GPM *this, char *path); static int Hardware_MouseDrivers_GPM_Close(Hardware_MouseDrivers_GPM *this); static int Hardware_MouseDrivers_GPM_Poll(Hardware_MouseDrivers_GPM *this); static int Hardware_MouseDrivers_GPM_Open(Hardware_MouseDrivers_GPM *this, char *path) { printf("GPM: Opening %s...\n", path); this->path = path; } static int Hardware_MouseDrivers_GPM_Close(Hardware_MouseDrivers_GPM *this) { printf("GPM: Closing %s...\n", this->path); } static int Hardware_MouseDrivers_GPM_Poll(Hardware_MouseDrivers_GPM *this) { printf("GPM: Polling %s...\n", this->path); } Hardware_MouseDriver Hardware_MouseDrivers_GPM_Methods = { .open = Hardware_MouseDrivers_GPM_Open, .close = Hardware_MouseDrivers_GPM_Close, .poll = Hardware_MouseDrivers_GPM_Poll }; /* DevInput */ typedef struct _Hardware_MouseDrivers_DevInput { char *path; } Hardware_MouseDrivers_DevInput; static int Hardware_MouseDrivers_DevInput_Open(Hardware_MouseDrivers_DevInput *this, char *path); static int Hardware_MouseDrivers_DevInput_Close(Hardware_MouseDrivers_DevInput *this); static int Hardware_MouseDrivers_DevInput_Poll(Hardware_MouseDrivers_DevInput *this); static int Hardware_MouseDrivers_DevInput_Open(Hardware_MouseDrivers_DevInput *this, char *path) { printf("DevInput: Opening %s...\n", path); this->path = path; } static int Hardware_MouseDrivers_DevInput_Close(Hardware_MouseDrivers_DevInput *this) { printf("DevInput: Closing %s...\n", this->path); } static int Hardware_MouseDrivers_DevInput_Poll(Hardware_MouseDrivers_DevInput *this) { printf("DevInput: Polling %s...\n", this->path); } Hardware_MouseDriver Hardware_MouseDrivers_DevInput_Methods = { .open = Hardware_MouseDrivers_DevInput_Open, .close = Hardware_MouseDrivers_DevInput_Close, .poll = Hardware_MouseDrivers_DevInput_Poll }; /* Test drivers */ void TestDriver(Hardware_MouseDriver driver, void *data) { /* Access the driver using a generic interface * (Hardware_MouseDriver) */ driver.poll(data); } void main() { Hardware_MouseDrivers_GPM gpm; Hardware_MouseDrivers_DevInput devinput; Hardware_MouseDrivers_GPM_Open(&gpm, "/dev/gpmctl"); Hardware_MouseDrivers_DevInput_Open(&devinput, "/dev/input/mice"); TestDriver(Hardware_MouseDrivers_GPM_Methods, &gpm); TestDriver(Hardware_MouseDrivers_DevInput_Methods, &devinput); Hardware_MouseDrivers_GPM_Close(&gpm); Hardware_MouseDrivers_DevInput_Close(&devinput); } 
+4
source share
2 answers

Passing assignments to the appropriate types (function pointers with void *, not your instance pointer):

  .open= (int (*)(void*, char *))Hardware_MouseDrivers_GPM_Open; 

Or create a type and use it in defining and initializing the structure:

 typedef int (*openfcnt_t)(void*, char *); typedef struct _Hardware_MouseDriver { openfnct_t open; } Hardware_MouseDriver; 

and then

  .open= (openfnct_t)Hardware_MouseDrivers_GPM_Open; 

EDIT:

Thinking that the simplest and least complicated way for program C would be:

  .open= (void *)Hardware_MouseDrivers_GPM_Open; 
+5
source

I assume that the obvious answer to this question is the question "why not fix the code to use the correct pointer type"?

EDIT

Well, I understand that you don’t want to complicate the code unnecessarily, but I don’t think that this is most of the complexity or even unnecessary.

Look at the open field in the Hardware_MouseDriver structure, which should be a pointer to a function that takes a pointer to void as its first argument.

To initialize this field, you use a pointer to the Hardware_MouseDrivers_GPM_Open function, and in another place a pointer to the Hardware_MouseDrivers_DevInput_Open function. None of them accepts a pointer to void as their first argument, and this, of course, is what the compiler warns about.

Now, if the void pointer is the same size as these pointers, and there are no other surprising differences between how they are stored and processed, calls to these functions using the open pointer will work as expected. This is likely to happen, and I think that with this type of low-level code, it is unlikely that anyone will send it a TOPS-20 or something like that. But there is no guarantee that this will work, and it looks (to me) weird. (And to the compiler, obviously!)

So, I suggest changing the code as follows:

 static int Hardware_MouseDrivers_GPM_Open(Hardware_MouseDrivers_GPM *this, char *path) { printf("GPM: Opening %s...\n", path); this->path = path; } 

just a little trickier:

 static int Hardware_MouseDrivers_GPM_Open(void *arg1, char *path) { Hardware_MouseDrivers_GPM *this = arg1; printf("GPM: Opening %s...\n", path); this->path = path; } 

I think this change would be simpler and less complicated than (1) disabling warnings, (2) documenting so readers can understand why this warning should not be important here, (3) documenting it is still why your readers really consider that you know what you are doing, and (4) handle problems that may arise if someone really portes your code to TOPS-20.

+1
source

All Articles