Domanda

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!

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top