Pregunta

I am trying to port code from 32b to 64bit and i am stuck on this one error.

      printf ("STUFF 0x%x ",(u_int32_t)val)

On porting i get an error saying %x expects argument of type unsigned int and argument 2 is of type "long unsigned int". So taking this error into account i make the change of %x to %lx

      printf ("STUFF 0x%lx ",(u_int32_t)val)

format %x expects argument of type long unsigned int and argument 2 is of type unsigned int. This is really confusing.

Now i do something like this and it works.

      printf ("STUFF 0x%x ",(u_int32_t)(size_t)val)

I am not sure why this solves the problem too. Now sure what is the right way to fix this error.

Adding one more question here.

So u have a structure st;

   st *a = NULL;
   u_int32_t = B

   a = (st *)B;

I get a error here saying "cast to pointer from integer of different size". I fix this by doing something like

  a = (st *)(size_t)B;

Is this the right way of doing it ?

¿Fue útil?

Solución

If you want to print a pointer then use the "%p" format specifier:

printf("STUFF %p\n", val);

See e.g. this reference.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top