[comp.lang.c] difference between 'char *arr' and 'char arr []'

asylvain@felix.UUCP (Alvin E. Sylvain) (10/02/90)

In article <0975@sheol.UUCP> throopw@sheol.UUCP (Wayne Throop) writes:
>> From: kuan@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!!"--=--=--=-

andras@alzabo.uucp (Andras Kovacs) (10/05/90)

In article <151805@felix.UUCP> asylvain@felix.UUCP (Alvin E. Sylvain) writes:
>In article <0975@sheol.UUCP> throopw@sheol.UUCP (Wayne Throop) writes:
>>> From: kuan@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

black@beno.CSS.GOV (Mike Black) (10/10/90)

Another quote from K&R original reference (Appendix A, paragraph 7.1):
"The expression E1[E2] is identical (by definition) to *((E1)+(E2))..."
2 paragraphs later, "...array names are CONVERTED to pointers" (my
emphasis).  A seeming conflict for something that is identical.  I
got bit on this trying to port some software to MicroSoft C.  It indeed
contained struct a[] in one section and struct *a as an extern in another.
It worked on an Amiga, Sun, and Dec 3100, but not on the PC.
	Here's where K&R says it (App A, par 14.3):
"Every time an identifier of array type appears in an expression, it is
converted into a pointer...".  Ergo, an extern of type pointer will not
get converted and unless type pointer and type array are equal will cause
problems.  
Mike...

--
-------------------------------------------------------------------------------
: usenet: black@beno.CSS.GOV   :  land line: 407-494-5853  : I want a computer:
: real home: Melbourne, FL     :  home line: 407-242-8619  : that does it all!:
-------------------------------------------------------------------------------

seanf@sco.COM (Sean Fagan) (10/12/90)

In article <49140@seismo.CSS.GOV> black@beno.CSS.GOV (Mike Black) writes:
>"The expression E1[E2] is identical (by definition) to *((E1)+(E2))..."
>2 paragraphs later, "...array names are CONVERTED to pointers" (my
>emphasis).  A seeming conflict for something that is identical.  

Uhm... no, it's not.  The syntax

	E1[E2]

works for pointers as well as arrays.  The normal use is

	ArrayName [ expr ];

Now, since array names are converted to pointers, we get

	(ArrayName_as_Pointer) [ expr ];

which is the same as

	*((ArrayName_as_Pointer) + (expr));

Where's the conflict?  Or don't people understand what was being said?  (I opt
for the latter, actually, but I tend to give novices one break 8-).)

-- 
-----------------+
Sean Eric Fagan  | "*Never* knock on Death's door:  ring the bell and 
seanf@sco.COM    |   run away!  Death hates that!"
uunet!sco!seanf  |     -- Dr. Mike Stratford (Matt Frewer, "Doctor, Doctor")
(408) 458-1422   | Any opinions expressed are my own, not my employers'.