C'mon, guys! (Really, pointer pedagogy)

John Owens jso at edison.UUCP
Fri Jun 27 04:46:36 AEST 1986


In article <487 at cubsvax.UUCP>, peters at cubsvax.UUCP writes:
> 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.
> Peter S. Shenkin	 Columbia Univ. Biology Dept., NY, NY  10027

Maybe it's my machine-language heritage showing, but I've always found
it least confusing to think of "pi" as "the contents of pi" (which is
a pointer), "*pi" as "that which (the contents of) pi points to",
"int i" as declaring i to contain an int, and "int *pi" as declaring
pi to contain a pointer to an int.

The crucial difference is in the notions of "to contain" and "to be".
The trick is that "int i" does not mean that i *is* an int, but that i
*is* a variable which contains an int.  The expression "i" is, of
course, an int: the int which i contains.

As far as the syntax of the declaration goes, this has proven useful:

	int  pi;	(the expression)   pi    is an int
	int *pi;	(the expression)  *pi    is an int
	int  pi[2];	(the expression)   pi[i] is an int
	int *pi();	(the expression)  *pi()  is an int

	John Owens @ General Electric Company	(+1 804 978 5726)
	edison!jso%virginia at CSNet-Relay.ARPA		[old arpa]
	edison!jso at virginia.EDU				[w/ nameservers]
	jso at edison.UUCP					[w/ uucp domains]
	{cbosgd allegra ncsu xanth}!uvacs!edison!jso	[roll your own]



More information about the Comp.unix mailing list