Search code examples
cgcc-warning

gcc throwing -Wint-to-pointer-cast despite the variable types are the same(from multiboot_uint64_t to multiboot_uint64_t*)


Here's the code that triggers the warning(public_mbd is a struct multiboot_info):

multiboot_uint64_t* fadr;
fadr=(multiboot_uint64_t*)public_mbd->framebuffer_addr;

the definition of framebuffer_addr and the struct is as follow:

struct multiboot_info{
...
multiboot_uint64_t framebuffer_addr;
...
} 

gcc said:

warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]

I'm getting really confused by that. Isn't fadr a multiboot_uint64_t*? They are the same size!


Solution

  • This expression

    public_mbd->framebuffer_addr
    

    is of type

    multiboot_uint64_t
    

    You attempt to cast it to type (multiboot_uint64_t*), i.e. a pointer to a multiboot_uint64_t. It appears that on your architecture the types multiboot_uint64_t and pointers have different sizes, but that is not really important; direct casting from a type to a pointer to that type is almost never the right thing to do.

    Perhaps you wanted the variable fadr to point to public_mbd->framebuffer_addr. This you can achieve by

    fadr = &(public_mbd->framebuffer_addr);
    

    Alternatively, of course, you may avoid pointers altogether and write

    multiboot_uint64_t fadr;
    fadr = public_mbd->framebuffer_addr;
    

    Another possibility is that the structure is incorrectly specified. If you want the framebuffer_addr field itself to be a pointer, you would write

    struct multiboot_info{
      ...
      multiboot_uint64_t* framebuffer_addr;
      ...
    }