[comp.lang.c] Pointers and Arrays

nevin1@ihlpf.ATT.COM (00704a-Liber) (01/30/88)

In article <8728@ism780c.UUCP> marv@ism780.UUCP (Marvin Rubenstein) writes:
>BTW, the definition of pointer difference in K&R is "if p and q point to
>members of the same array , p-q is the number of elements between p and q."

I opened up my K&R, skimmed all of chapter 5 (Pointers and Arrays), and could
not find this anywhere!  (Maybe I didn't look hard enough :-))  I don't think
this is how K&R defined it (see more comments below).
>
>One way to interpret this is the following:
>
>     el1  el2  el3
>      ^         ^
>      |         |
>      p         q
>
>As can be seen from the diagram the number of elements "between" p and q is
>1 (not 2).  Furthermore, the number of elements between p and q is clearly
>the same as the number of elements between q and p.  i.e, p-q == q-p and no
>overflow is possible.  I hope the defininition in the proposed standard is
>less ambigious than the wording in K&R.

Suppose q pointed to el2 instead of el3.  Would q-p === 0?  And if this were
true (which I maintain isn't), then what would q-p be if p and q pointed to the
same element?

Also, p-q is not equal to q-p (except in the trivial case where q === p).
There is no implied absolute value taken when subtracting pointers.
(p-q) === -(q-p)
For justification of what I am saying, I submit the following program:
==============================================================================
	main()

	{
	int	array[9];
	int	*p1;
	int	*p2;

	p1 = &array[3];
	p2 = &array[7];
	printf("%d\t", (int)(p1 - p2));
	printf("%d\n", (int)(p2 - p1));
	}
==============================================================================
When compiled and executed, this program produces:
	-4	4
QED


I don't know where your ideas came from, but they certainly weren't from K&R.
Maybe you were reading the new draft standard for ANSI C :-) :-)!!
-- 
 _ __			NEVIN J. LIBER	..!ihnp4!ihlpf!nevin1	(312) 510-6194
' )  )				"The secret compartment of my ring I fill
 /  / _ , __o  ____		 with an Underdog super-energy pill."
/  (_</_\/ <__/ / <_	These are solely MY opinions, not AT&T's, blah blah blah

daveb@laidbak.UUCP (Dave Burton) (01/31/88)

In article <3546@ihlpf.ATT.COM> nevin1@ihlpf.UUCP (00704a-Liber,N.J.) writes:
>In article <8728@ism780c.UUCP> marv@ism780.UUCP (Marvin Rubenstein) writes:
>>BTW, the definition of pointer difference in K&R is "if p and q point to
>>members of the same array , p-q is the number of elements between p and q."
>
>I opened up my K&R, skimmed all of chapter 5 (Pointers and Arrays), and could
>not find this anywhere!  (Maybe I didn't look hard enough :-))  I don't think
>this is how K&R defined it (see more comments below).

K&R, Chapter 5, page 98, Paragraph just above last example:
"Pointer subtraction is also valid: if p and q point to members of the
 same array, p-q is the number of elements between p and q."

(Gee, doesn't this look familiar? :-)

>>One way to interpret this is the following:
>>
>>     el1  el2  el3
>>      ^         ^
>>      |         |
>>      p         q
>>
>>As can be seen from the diagram the number of elements "between" p and q is
>>1 (not 2).  Furthermore, the number of elements between p and q is clearly
>>the same as the number of elements between q and p.  i.e, p-q == q-p and no
>>overflow is possible.

At least the way you describe it.
But K&R obviously meant otherwise. Read the example again.

If you really *must* cling to your rendering of "between", just
think of p and q pointing to the _middle_ of their objects, so
the distance between them is 1/2 + 1 + 1/2 = 2.
:-) :-) :-)

I'll agree the wording in K&R was/is poor.
I like H&S's description better: (page 166, first paragraph)
"Given two pointers p and q of the same type, the difference p-q is an integer
 k such that adding k to q yields p. ...
 The subtraction operator may produce unpredictable effects if overflow occurs
 ... or if either operand is a pointer."

>Also, p-q is not equal to q-p (except in the trivial case where q === p).

Such a cute little operator, "===" :-)
-- 
--------------------"Well, it looked good when I wrote it"---------------------
 Verbal: Dave Burton                        Net: ...!ihnp4!laidbak!daveb
 V-MAIL: (312) 505-9100 x325            USSnail: 1901 N. Naper Blvd.
#include <disclaimer.h>                          Naperville, IL  60540

chris@mimsy.UUCP (Chris Torek) (01/31/88)

In article <1329@laidbak.UUCP> daveb@laidbak.UUCP (Dave Burton) writes:
>K&R, Chapter 5, page 98, Paragraph just above last example:
>"Pointer subtraction is also valid: if p and q point to members of the
> same array, p-q is the number of elements between p and q."

This section of K&R is descriptive, not prescriptive.  What does
the appendix have to say?

At any rate, the number of elements `between' p and q (via p-q) in

	+---+---+---+---+
	| 0 | 1 | 2 | 3 |
	+---+---+---+---+
	 q       p

is easily seen as -2, if you just turn your brain around. :-)
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris

news@ism780c.UUCP (News system) (02/02/88)

I got lost in all the >'s

I (Marv Rubinstein) wrote:
BTW, the definition of pointer difference in K&R is "if p and q point to
members of the same array , p-q is the number of elements between p and q."

Some one else wrote:
>I opened up my K&R, skimmed all of chapter 5 (Pointers and Arrays), and could
>not find this anywhere!  (Maybe I didn't look hard enough :-))  I don't think
>this is how K&R defined it (see more comments below).

You didn't :-)  Perhaps you should have looked in the index first.  In the
index on page 226 one finds the entry:

    "pointer subtraction 98"

On page 98 one finds in the last paragraph:
          "Pointer subtraction is also valid:  if p and q point to members
	   of the same array , p-q is the number of elements between p and q."

My reason for bring up this whole issue was to point out the the term
"between" used here is fuzzy and could lead one to confusion.  I was just
asking if the language in the standard was less ambigious. (Doug answered
that it is less ambigious).  Also I once had to correct a compiler that was
implemented such that p-q was the same as q-p (p and q pointers to the same
array).  When I found the wording on page 98 I asked several colleagues "how
many integers are there between 1 and 3 and how many integers are there
between 3 and 1?" most answered "one" to both questions.

       Marv Rubinstein.  -- Interactive Systeme