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

Andras Kovacs andras at alzabo.uucp
Fri Oct 5 09:44:06 AEST 1990


In article <151805 at felix.UUCP> asylvain at felix.UUCP (Alvin E. Sylvain) writes:
>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!!"--=--=--=-



 Sorry to quote the whole article but I like it :-)

 My K&R Second Edition ANSI C book says:
  "In C, there is a strong relationship between pointers and arrays, strong
 enough that pointers and arrays should be DISCUSSED simultaneously."
 (Emphasis by me).
 Now, it is entirely possible that Alvin's copy uses the word TREATED but then
 someone has to understand that K&R talks about "we would like to talk about
 them here together 'cause they are quite similar concepts" as opposed to
 "you can deal with them the same, just two fancy notations".

 As far as the *challenge* goes, I think I found something broader than Alvin
asks for:
 "There is one difference between an array name and a pointer that must be
 kept in mind. A pointer is a variable, so pa=a and pa++ are legal. But an
 array name is not a variable; constructions like a=pa and a++ are illegal."
 I do not think it's nice to flame K&R with so much effort :-)

 Andras



More information about the Comp.lang.c mailing list