[net.lang.c] Fun with * and &

allbery@ncoast.UUCP (Brandon Allbery) (06/23/86)

Expires:

Quoted from <487@cubsvax.UUCP> ["Re: C'mon, guys! (Really, pointer pedagogy)"], by peters@cubsvax.UUCP...
+---------------
| Just a quick remark.  When I was learning C, I understood that "*pi" meant "the
| contents of pi," but somehow had difficulty conceptualizing why the declaration
| "int *pi;" declares pi as a pointer to an int;  that is, I knew it was a
| convention I had to memorize, but it didn't seem mnemonic to me.  Then, about
| a month ago, revelation!:  read this as "the contents of pi is an integer;"
| which implies, "pi is that which contains (or points to)" an integer.  Somehow 
| it made thinking about the declarations easier.  It's occurred to me that maybe
| everyone else in the world sees this from day 1, but for us dumb folks, having
| this reading pointed out would probably make the learning process easier....
+---------------

Easy:  Use the Algol 68 method.  (Snide remarks about sh and adb source to
/dev/null or /dev/bourne, please.)  Translate * as ``REF''.  Then they look
like:

		int *pi;	->	int REF pi;
		x = *pi;	->	x = REF pi;

REF is, of course, short for ``reference'', which is just another word for
``pointer''.  (Note that ``x = *pi'' is really a DEREFERENCE, since you're not
merely using the ``refrence'' to the interger, but the integer itself.  That's
the confusion of C.  ``*'' -> ``REF'' is a way to remember it.)

--Brandon
-- 
ihnp4!sun!cwruecmp!ncoast!allbery ncoast!allbery@Case.CSNET ncoast!tdi2!brandon
(ncoast!tdi2!root for business) 6615 Center St. #A1-105, Mentor, OH 44060-4101
Phone: +01 216 974 9210      CIS 74106,1032      MCI MAIL BALLBERY (part-time)

ark@alice.UucP (Andrew Koenig) (06/23/86)

> Easy:  Use the Algol 68 method.  (Snide remarks about sh and adb source to
> /dev/null or /dev/bourne, please.)  Translate * as ``REF''.  Then they look
> like:
>
>		int *pi;	->	int REF pi;
>		x = *pi;	->	x = REF pi;
>
> REF is, of course, short for ``reference'', which is just another word for
> ``pointer''.  (Note that ``x = *pi'' is really a DEREFERENCE, since you're not
> merely using the ``refrence'' to the interger, but the integer itself.  That's
> the confusion of C.  ``*'' -> ``REF'' is a way to remember it.)

But that's not how Algol 68 works.  Instead, you write:

		REF INT pi;

which says that pi is bound to a reference to a reference to an
integer (less formally, a variable of type REF INT).  You can
also say:

		INT x;

which defines x as an integer variable (formally a REF INT), and then:

		x := pi;

which implicitly dereferences pi, or

		x := INT (pi);

which explicitly dereferences it.

The easy way to remember how C pointer declarations work is that

		int x;

says that x is an int, and

		int *x;

says that *x is an int, so x is a pointer to int.

aptr@ur-tut.UUCP (The Wumpus) (06/24/86)

In article <1250@ncoast.UUCP> allbery@ncoast.UUCP (Brandon Allbery) writes:
>Expires:
>
>Quoted from <487@cubsvax.UUCP> ["Re: C'mon, guys! (Really, pointer pedagogy)"], by peters@cubsvax.UUCP...
>+---------------
>| Just a quick remark.  When I was learning C, I understood that "*pi" meant "the
>| contents of pi," but somehow had difficulty conceptualizing why the declaration
>| "int *pi;" declares pi as a pointer to an int;  that is, I knew it was a
>| convention I had to memorize, but it didn't seem mnemonic to me.  Then, about
>| a month ago, revelation!:  read this as "the contents of pi is an integer;"
>| which implies, "pi is that which contains (or points to)" an integer.  Somehow 
>| it made thinking about the declarations easier.  It's occurred to me that maybe
>| everyone else in the world sees this from day 1, but for us dumb folks, having
>| this reading pointed out would probably make the learning process easier....
>+---------------
>
>Easy:  Use the Algol 68 method.

If you have worked with Forth, the idead of pointers to variable locations
and having to give scanf an address to put the returned value into is
natural because variables in Forth are really just address pointers.

:-)
The Wumpus

ecf_bdw@jhunix.UUCP (Dwight Wilson) (06/26/86)

In article <1250@ncoast.UUCP> allbery@ncoast.UUCP (Brandon Allbery) writes:
>Expires:
>
>Quoted from <487@cubsvax.UUCP> ["Re: C'mon, guys! (Really, pointer pedagogy)"], by peters@cubsvax.UUCP...
>+---------------
>| Just a quick remark.  When I was learning C, I understood that "*pi" meant "the
>| contents of pi," but somehow had difficulty conceptualizing why the declaration
>| "int *pi;" declares pi as a pointer to an int;  that is, I knew it was a
>| convention I had to memorize, but it didn't seem mnemonic to me.  Then, about
>| a month ago, revelation!:  read this as "the contents of pi is an integer;"
>| which implies, "pi is that which contains (or points to)" an integer.  Somehow 
>| it made thinking about the declarations easier.  It's occurred to me that maybe
>| everyone else in the world sees this from day 1, but for us dumb folks, having
>| this reading pointed out would probably make the learning process easier....
>+---------------
>
>Easy:  Use the Algol 68 method.  (Snide remarks about sh and adb source to
>/dev/null or /dev/bourne, please.)  Translate * as ``REF''.  Then they look
>like:
>
>		int *pi;	->	int REF pi;
>		x = *pi;	->	x = REF pi;
>
>REF is, of course, short for ``reference'', which is just another word for
>``pointer''.  (Note that ``x = *pi'' is really a DEREFERENCE, since you're not
>merely using the ``refrence'' to the interger, but the integer itself.  That's
>the confusion of C.  ``*'' -> ``REF'' is a way to remember it.)

Well I don't really see this as very helpful, all you've done is replace
the symbol '*' with the word 'REF'.  Although this is more mnemonic I
don't think it is more helpful conceptually.

More helpful is something like this.

      #define pointer_to_int  int *

then the definition

      pointer_to_int pi;

expands into:

      int * pi;  (same as int *pi, the space is unimportant).

The form 'int *pi' is confusing to the uninitiated since it says
that *pi is an integer, so therefore pi is a pointer to an integer.
the form 'pointer_to_int pi' directly states that pi is a pointer.

Incidently you can use  

       #define pointer_to(X)  X *

to create a general pointer_to declaration.  Of course this horribly
wordy on convoluted declarations.

      
                                                      -Dwight

rbj@icst-cmr (Root Boy Jim) (06/26/86)

> Just a quick remark.  When I was learning C, I understood that "*pi"
> meant "the contents of pi," but somehow had difficulty conceptualizing
> why the declaration "int *pi;" declares pi as a pointer to an int;
> that is, I knew it was a convention I had to memorize, but it didn't
> seem mnemonic to me. 

It may help to look at as a declaration that (*p) is an int. Therefore,
you can infer that p is a pointer to an int. See K&R, page 90.

	(Root Boy) Jim Cottrell		<rbj@icst-cmr.arpa>
	Hold the MAYO & pass the COSMIC AWARENESS...