home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!spool.mu.edu!agate!ucbvax!lrw.com!leichter
- From: leichter@lrw.com (Jerry Leichter)
- Newsgroups: comp.os.vms
- Subject: re: C pointer question
- Message-ID: <9212272214.AA24880@uu3.psi.com>
- Date: 27 Dec 92 21:02:43 GMT
- Sender: daemon@ucbvax.BERKELEY.EDU
- Distribution: world
- Organization: The Internet
- Lines: 51
-
- Sigh. In an earlier flamey message, I wrote that the two pieces of code,
-
- void foo(char a[])
- {
- bar(a);
- }
-
- and
-
- void foo(char a[])
- {
- bar(&a);
- }
-
- were identical. Wrong, Leichter, wrong. If you're going to flame, just
- flame, don't throw in a quick remark without thinking it through (which is
- unlikely in the midst of flaming.)
-
- As Neil Readwin has already pointed out to me in a private message (and I'm
- sure plenty more people will be pointing it out, too), in fact, the function
- "void foo(char a[])" is indistiguishable from "void foo(char *a)" - one of C's
- more annoying little hacks. Despite the appearance of the declaration, "a"
- ends up typed as "pointer to char", NOT as "array of char". (ANSI Standard,
- Section 3.7.1 (under Semantics): "A decla- ration of a parameter as 'array of
- type' shall be adjusted to 'pointer to type'.") So the two definitions ARE
- different: The first passes "a"'s value (foo's argument, of type "char *");
- the second passes a pointer to "a" itself (of type "char **").
-
- As to the original program that this arose in, the author wrote:
-
- bar wants a char *, foo is passed a char *.
-
- If bar wants a char *, then the first piece of code is correct. The second
- passes a char **. Since it was the second (type-incorrect) code that was
- alleged to actually work, my opinion of the level of programming involved here
- remains unchanged.
-
- (For those who like chapter and verse: My original comment that, if "a"
- really has type "array of T", then "a" is not an lvalue, is correct according
- to the ANSI Standard, Section 3.2.2.1: "Except when it is the operand of the
- sizeof operator or the unary & operator ... an lvalue that has type 'array of
- type' is converted to an expression that has type 'pointer to type' ... [that]
- is not an lvalue." The exception for unary "&" allows "a" and "&a" to be
- almost equivalent. (Technically, the first has type "pointer to T" while the
- second has type "pointer to array of T". The values are the same, but the
- difference in type will be visible as a different scaling value if you add an
- integer to these pointers. This is actually a change from K&R C, where the
- exception did not "officially" exist, and where "&a" if allowed was taken to
- mean EXACTLY the same thing as "a" itself.))
- -- Jerry
-
-