1
votes

I want to print addresses of void pointer

u32 j;
for (j = 0; j < sizeof(struct queue_header); j += 4) 
{
    printf("0x%x ",(u32 *)((u32 *)q->q_hdr + j)); //q_hdr  is a void pointer        
}

but that type conversion is giving error:

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

can you please tell me how can I print the address.

2
j += 4 is wrong: if you cast to u32 *, you are already counting in terms of u32s, so j++ would be right. - glglgl
Side note: one (u32*) casting is sufficient in this case. - barak manos
Side note #2: Use %x for int (or smaller) variables, %lx for long int variables, %llx for long long int variables, and %p for Type* (pointer) variables. - barak manos

2 Answers

8
votes

That's what %p is for: printing pointers.

printf("%p ", (void *)((u32 *)q->q_hdr + j));

should do what you want.

0
votes

Is your code running on a 64-bit platform (Intel Core 2 Duo, i7)?

Perhaps "%lx" would work for the printf.

You'll need to cast the argument to printf to a 64-bit integer as well.