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
Printable View
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;
}
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.