difference between 'char *arr' and 'char arr []'

Alvin E. Sylvain asylvain at felix.UUCP
Tue Oct 2 08:49:18 AEST 1990


In article <0975 at sheol.UUCP> throopw at sheol.UUCP (Wayne Throop) writes:
>> 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.

I'd like to point out what is IMHO the ultimate source of confusion.

I have quoted here from the *near original* K&R, "The C Programming
Language", copyright 1978, 7th printing, page 93:
''
5.3 Pointers and Arrays
   In C, there is a strong relationship between pointers and arrays, strong
enough that pointers and arrays should be treated simultaneously.
...
  [goes on to tell how pa[i] is identical to *(pa+i)]
...
   As formal parameters in a function definion,
      ------ ----------      -------- --------
        char s[];
and
        char *s;
are exactly equivalent; which one should be written is determined largely by
    ------- ----------   [emphasis mine]
how expressions will be written in the function.
...
''
Notice how technically, this is quite correct (heck, this is *bible* of C,
it ought to be at least *technically* correct), but, unless you're a
seasoned veteran, it's durnedably misleading.  There is no discussion
here that says you *can't* do this when defining extern's across function
boundaries.

My point here is that K&R devote three full pages to telling us how
equivalent pointers and arrays are, but *only one line* telling us
"oh, BTW, the two declarations are only identical in function formal
parameter definitions".

I liken it to saying, "oh sure, you can go through the green light!
That's what the light is there for!"  (14 pages of how to go through the
green light deleted for brevity's sake)   (whoops ... forgot to mention
that nasty ol' *red* light!  Why do we keep having these accidents at
intersections?  I know, lack of proper driver's ed!)

I challenge anybody (seriously, I don't want to do the research myself)
to find *one reference* in K&R that *explicitly* says you *can not* use
pointer declarations in one function and _extern_ array declarations in
another (and, of course, vice-versa).  Assuming you can find it, why
isn't it discussed at all in 5.3?  (Only K&R can answer that one!)
--
------------------------------------------------------------------------
"I got protection for my    |               Alvin "the Chipmunk" Sylvain
affections, so swing your   |   Natch, nobody'd be *fool* enough to have
bootie in my direction!"    |   *my* opinions, 'ceptin' *me*, of course!
-=--=--=--"BANDWIDTH??  WE DON'T NEED NO STINKING BANDWIDTH!!"--=--=--=-



More information about the Comp.lang.c mailing list