What is wrong with this code, Can't I add pointer and a unsigned value together?

StackOverflow https://stackoverflow.com/questions/21418070

  •  04-10-2022
  •  | 
  •  

문제

Test platform is Linux 32 bit.

I found a bug in my code, and I don't know why....

I simplified this code and put it here:

unsigned int aa  = 0;
unsigned int array[10000];
unsigned int* ptr = array + 2000;

printf("aa: %d ", aa);   // value 1
printf("ptr: %d \n", ptr);  //value 2
printf("aa+ptr: %d \n", aa + ptr);  // value 3

compiler is gcc version 4.6.3

It seems silly but I don't understand why value3 != value1 + value2

Could anyone give me some help?

Thank you!

도움이 되었습니까?

해결책

In the second and third cases, you're passing a pointer to printf, but using %d as a formatter. You need %p.

ints and pointers are not interchangeable.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top