Code:void main() { int *p,*q; p=(int*)1000; q=(int*)2000; printf("%d",(q-p)); } why it outputs 500.anyone know the reason??help me
Code:void main() { int *p,*q; p=(int*)1000; q=(int*)2000; printf("%d",(q-p)); } why it outputs 500.anyone know the reason??help me
A much safer version which doesn't rely on undefined pointer behaviour.
Then try this oneCode:#include <stdio.h> int main ( ) { /* Verily, main doest always return an int */ int arr[100]; int *p = &arr[10]; int *q = &arr[40]; printf("%d\n",(q-p)); return 0; }
Notice anything?Code:#include <stdio.h> int main ( ) { /* Verily, main doest always return an int */ double arr[100]; double *p = &arr[10]; double *q = &arr[40]; printf("%d\n",(q-p)); return 0; }
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
On your box, int * is 2 bytes. So two addresses which are 1000 bytes apart are 500 int *'s apart.
See the FAQ regarding void main()... On my 32bit system (an int is 4 bytes) the result is 250, so the two addresses are 1000 bytes appart, or 4 * 250 bytes.