Re: Pointers in C



"Raman" <ramanchalotra@xxxxxxxxx> writes:
On Feb 21, 11:18 am, Ian Collins <ian-n...@xxxxxxxxxxx> wrote:
Praveen wrote:
Hi,

I came across a program as follows
main()
{
int x, y ;
int *p1 = &x;
int *p2 = &y;
printf("%d\n", p1-p2);
}

The output of the above program is 1.
Can some one explain me how it is 1.

Because that's what your compiler gives as the answer, subtracting two
unrelated pointers is undefined behaviour.
Can Some onr tell why these are unrelated?

Because they point to distinct objects.

Here's what the standard says in the section on relational operators
(<, <=, >, >=) (C99 6.5.8p5):

When two pointers are compared, the result depends on the relative
locations in the address space of the objects pointed to. If two
pointers to object or incomplete types both point to the same
object, or both point one past the last element of the same array
object, they compare equal. If the objects pointed to are members
of the same aggregate object, pointers to structure members
declared later compare greater than pointers to members declared
earlier in the structure, and pointers to array elements with
larger subscript values compare greater than pointers to elements
of the same array with lower subscript values. All pointers to
members of the same union object compare equal. If the expression
P points to an element of an array object and the expression Q
points to the last element of the same array object, the pointer
expression Q+1 compares greater than P. In all other cases, the
behavior is undefined.

(A non-array object is treated as an array of one element.)

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.



Relevant Pages

  • Re: Test if pointer points to allocated memory
    ... >> or one past the same object or array. ... When two pointers are compared, the result depends on the relative ... pointers to structure members declared later compare ... > The typical implementation of automatic storage is on the program stack. ...
    (comp.lang.c)
  • Re: Array comparison
    ... pointers are only going to compare their object pointers ... pointers and not the contents of the strings. ... things like array of Char, bytes, words, dword, int64's etc.. ...
    (alt.comp.lang.borland-delphi)
  • Re: Questions regarding specialized malloc()/free() replacements
    ... that range *must* compare not-equal. ... Equal, not equal, sure, but the others require that the pointers be ... | or both point one past the last element of the same array object, ... | and pointers to array elements with larger subscript values compare ...
    (comp.lang.c)
  • Re: Differance between Array and Pointers
    ... Well, except that arrays tend to be much larger than pointers, and the ... An array is a contiguous region of memory containing N elements of M ... indexing vs. a loop). ...
    (comp.arch.embedded)
  • [RFCv2][PATCH] flexible array implementation
    ... I call it a flexible array. ... storage for pointers to the second level. ... all locking must be provided by the caller. ... make sure to pass in &ptr instead of ptr. ...
    (Linux-Kernel)