r/C_Programming • u/aliathar • Aug 18 '26
Negative value in a pointer question.
Please look at this code. if i define the PTRTYPE as int, it stops working, while doing a uint, it does work...
void initILAPoll(debugBridge_t **d, PTRTYPE ptr){
*d = (debugBridge_t *)ptr; // base address of the DEBUG_BRIDGE peripheral
cb_init(cb, local_memory, bufferLength);
sprintf(xvcInfo, "xvcServer_v1.0:%d\n", MAX_WINDOW_SIZE);
}
the usage in main code is done like this
initILAPoll(&myD, 0x80000000);
//myD = (debugBridge_t *)0x80000000;
where the variable myD is a structure pointer.
if i print the address of myD, it give the correct address. Moreover, the disassembly of the code is also the same in case of int and uint. Can somebody explain what behavior is at play here>
2
Upvotes
View all comments
Show parent comment
0
u/flyingron Aug 19 '26 edited Aug 19 '26
Pointers are not integers and there are platforms C has existed on they were not and this is why all that stuff about comparing pointers require them to be within the same object.
Even when they are somewhat like integers, there's not necessarily a conversion that makes sense. I'll give you some examples. I've been involved in developing UNIX and C on a few mainframes and supercomputers. I have seen the partial word sizes encoded in the pointer, plus I've seen byte offsets encoded in word pointer machines in the high order bits (quite germain to this talk). You have to be careful doing conversions like:
int* -> uintptr_t -> long*
or
char* -> uintptr_t -> int*.