Array question

Paul G. Tobin pgt at hpfcso.FC.HP.COM
Fri Feb 15 03:36:59 AEST 1991


Michael Stefanik wrote:
* One thing to point out is that pointers and arrays are _not_ interchangable
* entities _except_ when you are passing them to functions.  The moral is this:
* when you mean to use a pointer, use a pointer; when you mean to use an array,
* use an array.

	Michael, can you clarify this a little bit?  I thought
pointers and arrays in C _were_ interchangeable, which is often quite
convenient.  On pg. 94 of the original K&R, they state (this is a
quote):

	"In fact, a reference to an array is converted by the compiler
to a pointer to the beginning of the array.  The effect is that an
array name _is_ a pointer expression."

On the previous page (93), section 5.3 opens with:

	"In C, there is a strong relationship between pointers and
arrays, strong enough that pointers and arrays really should be
treated simultaneously.  Any operation which can be achieved by array
subscripting can also be done with pointers.  The pointer version will
in general be faster but, at least to the uninitiated, somewhat harder
to grasp immediately."

It would seem that today's compilers probably somewhat close the speed
gap between array and pointer addressing, but I'd contend that they
*can* be freely interchanged.  It all depends on your personal style.
Use whatever mode seems more suited to the task at hand.

Sorry for the drift, but the previous responses have aptly answered
the question in the basenote.  3 responses without diverging from the
original topic is too much :-).

	Paul Tobin
	pgt at hpfipgt.fc.hp.com



More information about the Comp.lang.c mailing list