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