1

I need help with a compile error that I am getting.

The error is: error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]

on the following line

PRINT_INDENT("Inode2 PTR: %p\n", (void *)ptr);

Context

unsigned int ptr;
memcpy(&ptr, value, sizeof(ptr));
PRINT_INDENT("Inode2 PTR: %p\n", (void *)ptr);

#define PRINT_INDENT(...) ({int __j = indent_level; while (__j-- > 0) printf("  "); printf(__VA_ARGS__);})
jogojapan
  • 68,383
  • 11
  • 101
  • 131
user1253073
  • 374
  • 2
  • 6
  • 26

3 Answers3

2

Are you using a 64-bit platform with 32-bit ints? If so, the error is self-explanatory. ptr is a 32-bit variable, and you're casting it to a 64-bit void *. A workaround is to first cast it to an unsigned long long and then cast the result to a void *.

PRINT_INDENT("Inode2 PTR: %p\n", (void *)(unsigned long long)ptr);

But why are you doing this? Why not just print ptr as an unsigned int instead of casting it to a void * as follows:

PRINT_INDENT("Inode2 PTR: %u\n", ptr);
Praetorian
  • 106,671
  • 19
  • 240
  • 328
  • yes off course it was self explanatory, how did i miss that?:D thanks a bunch, first I didnt know that void* would be 64bits and I am on a 32-bit platform, so your suggestion helped. Thanks again. To answer the question, I am trying to compile an app that I need. So I havent even started running it to tell if there is a reason for this format or not. – user1253073 Oct 25 '12 at 03:53
  • @user1253073 It would be very unusual to have 64-bit pointers if you're on a 32-bit platform, unless of course, whatever target you're compiling for has 32-bit pointers and 16-bit ints – Praetorian Oct 25 '12 at 03:57
  • I apologize for the bad title, thank you all for the answers. – user1253073 Oct 25 '12 at 03:58
  • honestly, not sure if the app was written on a 64-bit platform and now i am trying to run it on a 32-bit plat. i am very new at C hence the newbie question. – user1253073 Oct 25 '12 at 04:01
1

You should use the uintptr_t type if you want to do this type of thing. It is a relatively new type available in the C99 and C++11 standards.

There's no guarantee that ints will be large enough to hold pointer values, and indeed on a 64-bit platform with 32-bit ints and 64-bit pointers they aren't.

memcpy(&ptr, value, sizeof(ptr));

If value is a pointer this line will only copy half of its 64-bit value.

Community
  • 1
  • 1
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
1

I am unsure why you need to cast an int to pointer; there may be no need if the purpose is just printing pointers.

However, if that kind of cast is necessary, then in order to be sure that the integer has the right size, C++11 provides an alias for an unsigned integer data type that is sufficiently large to hold a pointer:

std::uintptr_t

which is defined in the cstdint header (for C, the C99 standard defines this as uintptr_t in stdint.h as well).

Using this as your integer type (or casting existing integer types to this – but note there may be information loss if the original type is larger!) is the right approach if int/pointer casts are really required.

See related question: What is uintptr_t data type

Community
  • 1
  • 1
jogojapan
  • 68,383
  • 11
  • 101
  • 131