int a[5];
int *Ptr1, *Ptr2;
Ptr1 = a;
Ptr1++;
Ptr2 = a;
printf("Ptr1 : %p\n", Ptr1);
printf("Ptr2 : %p\n\n", Ptr2);
Ptr1 = Ptr1 - Ptr2;
---------------------
Ptr1 : 0012FF6C
Ptr2 : 0012FF68
Ptr1 : 00000001
Ptr2 : 0012FF68
----------------------
i couldnt understand, Ptr1 = Ptr1 - Ptr2; line is a pointer arithmetic,
and Ptr1 should be 0012FF6C, because Ptr1 is a[1] and Ptr2 is a[0]
a[1] - a[0] should be a[1] so 0012FF6C
probably i am wrong but i wanted to ask
Compile with warnings enabled.
The "%p" format expects an argument of type void*, which is not
compatible with int*. It may happen to work if void* and int* happen
to have the same representation (which they very commonly do), but you
can avoid any possible problems by converting the argument:
printf("Ptr1 : %p\n", (void*)Ptr1);
printf("Ptr2 : %p\n\n", (void*)Ptr2));
This is one of the few cases where a cast is actually appropriate.
Subtracting one pointer from another yields a result of the integer
type ptrdif_t. (This is valid only if both pointers point to elements
of the same array, which is the case here; otherwise it invokes
undefined behavior.) So you're trying to assign an integer value to a
pointer variable. The language allows you to convert an integer value
to a pointer type (the result is implementation-defined and very
likely not useful), but you can only do so with a cast; there is no
implicit conversion from integers to pointers (other than the special
case of a null pointer constant). So your assignment is a constraint
violation, and the compiler is required to issue a diagnostic message.
(If yours did so, you didn't bother to tell us.) The diagnostic can
be just a warning (if the compiler supports this implicit conversion
as an extension), but it's required for any conforming compiler. You
might have to pass some additional options to your compiler, for
example enabling certain warnings, to make it conforming.
Also, you posted only a code fragment; in particular, you didn't show
us all the code that produces the output you showed us. Next time, if
at all possible, please post a small, complete, self-contained,
compilable program. The fragment you posted happened to be clear
enough, but you can never be sure of that; quite often the seemingly
irrelevant piece of code that someone thinks isn't worth posting is
just the thing that causes or explains the problem.