Clement Poon
Clement Poon

Reputation: 79

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!

Upvotes: 0

Views: 235

Answers (2)

printf
printf

Reputation: 335

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;
  ...
}    

Upvotes: 2

kaylum
kaylum

Reputation: 14044

It's not faddr that is the problem. It is the cast that is the problem.

From the type it is clear that multiboot_uint64_t framebuffer_addr is 64 bit. And from the warning we can guess that pointers are 32 bit for your build. So the warning is telling you that you are casting a 64 bit int to a 32 bit pointer.

Upvotes: 2

Related Questions