r/C_Programming • • 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

43 comments sorted by

View all comments

Show parent comment

1

u/alkatori Aug 18 '26

is that guaranteed by spec? My recollection (or maybe it was just rule of thumb was).

char <= short <= int <= long <= long long

with the char being the smallest addressable unit in the hardware (I worked on a system that had 16-bit was the smallest addressable unit, lots of code assuming 8-bit bytes broke).

2

u/TheChief275 Aug 18 '26 edited Aug 18 '26

I think the spec introduced actual guaranteed number capabilities of the standard integers around the time of C99, but yes basically, int is only guaranteed to be >= short, so it can definitely be 16 bits on some platforms which is were the guaranteed range comes from. The minimum is also -32,767 instead of -32,768, because there is no guarantee for whether the integer is two's complement (not until C23 at least)

1

u/torsten_dev Aug 18 '26

POSIX guarantees CHAR_BIT == 8 but yes some evil systems exist where that's not the case.