C'mon, guys! (Really, pointer pe

jrv at siemens.UUCP jrv at siemens.UUCP
Fri Jun 20 23:25:00 AEST 1986



Peter Shenkin 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.  Somehow

	Interesting how the same words have completely different meanings
	for two people. The revelation which helps Peter keep pointers
	straight uses the exact words which helps me make the *distinction*
	between pointers and what they point to.

	For me contents of pi is a *pointer* to an integer. I do not make
	the association of "contains (or points to)" as the same thing. In
	fact I emphasis the difference.

	I agree that the declaration of pointers and their use is not
	consistent. Two statements which to the novice C programmer look
	very similar have different results:

		In a declaration:

			Int *pi = abcd;

		will store a value in the pointer variable pi.
		While in the middle of code:

			*pi = wxyz;

		will store a value where pi points.

	In the C classes I have taught this inconsistency did not appear to
	be a major stumbling point. When asked at the end of the semester
	what was the hardest topic in the course the overwhelming response
	is, "POINTERS, POINTERS, POINTERS". It was there general use not so
	much the declaration/initialization. Each semester I have added more
	material but have still not gotten to the point where the confusion
	level has been reduced to my satisfaction.

	I don't think that this is
	peculiar (sp?) to C. The extra level of indirection is a difficult
	concept to grasp whether it is in C or PASCAL or whatever. It is
	like riding a bicycle: once you know how you never forget; likewise
	once you understand pointers you know them for life. (There are
	probably several concepts which fall into this category. Recursion
	is another one that comes to mind.)

	(Jim, stop the semester ended over a month ago! In a minute the bell
	hasn't rung yet. :-) The one technique which I use often when I am
	attempting to keep a pointer staight is the "cover-up" technique.
	If I want to know what data type a variable is I go back to the place
	where it is declared and cover over what I am interested in and what
	remains is the data type of the object. For a simple example:

		Int *pi;

		given 'pi' cover this over in the declaration and you get
			'Int *'. So 'pi' is a pointer to an integer. 'pi'
			contains a pointer to an integer.

		or given '*pi' cover it in the declaration and you get 'Int'.
			So '*pi' is an integer. '*pi' contains an intger.
	
	Enough.

>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....
>
>Peter S. Shenkin	 Columbia Univ. Biology Dept., NY, NY  10027
>{philabs,rna}!cubsvax!peters		cubsvax!peters at columbia.ARPA
>


Jim Vallino
Siemens Research and Technology Lab.
Princeton, NJ
{allegra,ihnp4,seismo,philabs}!princeton!siemens!jrv



More information about the Comp.lang.c mailing list