Difference between "char *arr" and "char arr[]"

Wayne Throop throopw at sheol.UUCP
Sun Sep 23 08:27:51 AEST 1990


> From: kuan at iris.ucdavis.edu (Frank [Who me?] Kuan)
> Now, I always thought that "targ[]" and "char *targ" were equivalent.

As an aside, I took a survey a while back about this.  It turns out
that the reason for this frequent misconception is (essentially)
poor teaching and poor reference materials.  Essentially teachers
TEACH people this, sometimes inadvertantly.

My main suggestion to Frank Kuan is to read the "Frequently Asked
Questions" posting when it comes around again.  You will find the
answer to the [] vs * question, along with a slew of other things
that teachers (apparently) often mis-teach students.

> From: cpcahil at virtech.uucp (Conor P. Cahill)
> They are equivalent (with respect to referencing data) for just about all
> cases EXCEPT for global variables.

And automatic variables.  And objects in the malloc/free heap.  And
variables with static visibility/persistence.

I think that Conor may be taking "equivalent" to mean that the two
objects can appear in lexically identical references.  Which is correct.
But the semantics which apply to the two cases are distinct.

The fact is, there is only ONE place where the two are semantically
equivalent, and that is as definitions of formal parameters.  Again, see
the FAQ for details, but code generated for subscripting, "sizeof"
results, and in essence *everything* is different for these two
declarations, with only the one exception. 

So, while Conor is technically correct, I think it is much more
important to emphasize the differences here, rather than the similarities.
The similarities are superficial, and the differences important,
fundamental, and often overlooked.
--
Wayne Throop <backbone>!mcnc!rti!sheol!throopw or sheol!throopw at rti.rti.org



More information about the Comp.lang.c mailing list