So I was playing around with char*
and unsigned char*
pointers. And I came across this issue:
Code:
#include <stdio.h>
void func(unsigned int max) {
unsigned int* intptr = &max;
printf("%u - %pn", max, &max);
printf("%u - %pnn", *intptr, intptr);
printf("%u - %pn", *((char*)intptr), ((char*)intptr));
printf("%u - %pn", *((unsigned char*)intptr), ((unsigned char*)intptr));
}
int main(void)
{
unsigned int max1 = 0b00000000000000000000000001111111;
unsigned int max2 = 0b00000000000000000000000011111111;
func(max1);
printf("nnnn");
func(max2);
return 0;
}
Output:
127 - 0x7ffdfbd8177c
127 - 0x7ffdfbd8177c
127 - 0x7ffdfbd8177c
127 - 0x7ffdfbd8177c
255 - 0x7ffdfbd8177c
255 - 0x7ffdfbd8177c
4294967295 - 0x7ffdfbd8177c
255 - 0x7ffdfbd8177c
So here Little Endian is being used, and clearly char is overflowing at 128. However why does it overflow and print out the UINT_MAX
instead of -128?
New contributor
programk5er is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1