Is there any difference between (uintptr_t) and (uint8_t *)? Which is more portable?
Based on usage, can they be used interchangeably?
Is there any difference between (uintptr_t) and (uint8_t *)? Which is more portable?
Based on usage, can they be used interchangeably?
uintptr_t
is an unsigned integer large enough to contain a void *
, to which any pointer but a function pointer can be converted.
uint8_t *
is a pointer to a uint8_t
.
You ask if there are any differences, but I'm having problems coming up with any meaningful similarities. There just about as different as night and calendar.
I would say {signed,unsigned,} char*
as neither uint8_t
nor uintptr_t
are guaranteed to be defined, strictly speaking.
char_type *
is not interchangeable with uintptr_t
, however.
While you can use char_type *
to point at the bytes of an object, you can do wilder pointer arithmetic with uintptr_t
because then you don't necessarily get undefined behavior cases if you end up pointing at a wrong place.
E.g.:
#include <stdio.h>
#include <stdint.h>
int main(){
char a[]={11,22,33};
//for(char *p=a+2; p!=a-1;--p)
// printf("%d\n", *p); //undefined reverse iteration: can't point to &a[-1]
for(uintptr_t p=(uintptr_t)(a+2); p!=(uintptr_t)a-1;--p)
printf("%d\n", *(char*)p); //OK; (uintptr_t)&a[0] - 1 is just a number
}