> But you said it wont give above result
No, I said your program in post #1 wouldn't even compile.
When it's fixed, it produces the result you see.
> as you said below statement is waste of time.why???
Because
*ptr++; is the same as
ptr++;
The * bit is the bit which is the complete waste of time, and why you get the message
"warning: value computed is not used "
The same goes for the
*++ptr;
Because the pre and post increments are the same by the time the code hits the
;
the first 3 pointer increments and prints are just
1 2 3 4 5 6
|
ptr = ptr + 1;
printf("%d %d %d \n",ptr-p1,*ptr-arr1,**ptr);
ptr = ptr + 1;
printf("%d %d %d \n",ptr-p1,*ptr-arr1,**ptr);
ptr = ptr + 1;
printf("%d %d %d \n",ptr-p1,*ptr-arr1,**ptr);
|
> here subtracting two address.It has to print difference of the address
> not value.how it is printing 1 value
Because pointer subtraction knows about the types of pointers, and is bound to how array indexing works.
1 2 3
|
int a[10];
int n = 5;
int *pa = &a[n];
|
At this point, if you did
pa - a, you would get 5 as the answer.
Or maybe write it in a more basic form.
1 2 3
|
int a[10];
int n = 5;
int *pa = a + n; // what &a[n] means to the compiler.
|
Now it's just algebra.
pa = a + n can be rearranged as
n = pa - a
Try this as a fun exercise, what does it print?
1 2 3 4 5
|
#include <stdio.h>
int main ( ) {
int a[10];
printf("%zd\n", &a[7] - &a[2] ); // or just %d if your compiler is crusty
}
|