Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Question about extern declaration Date: Mon, 19 Sep 2016 11:46:49 -0700 Organization: None to speak of Lines: 52 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="945944de09706c9b4e29b53c9d2efdc2"; logging-data="4122"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19YE0Lfv2i5XOzszOGFGL59" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:7F7wUkvWGmVV4S+jsBRkEH2W0y4= sha1:NiE9B7vzr8oxRxWW0C0elW7FNjI= Xref: csiph.com comp.lang.c:93812 Nobody writes: > On Sun, 18 Sep 2016 23:06:47 +0000, Kenny McCormack wrote: > >> So, what, really, is the difference between the two forms (specifically, >> in an extern statement)? > > One is a pointer to a pointer, the other is an array of pointers. > > Arrays and pointers are equivalent in some contexts, but not all. Arrays and pointers are not equivalent in any context. An array expression is, in most contexts, converted to a pointer expression whose value is the address of the initial element of the array object. We can, for example, apply the unary "*" operator to the name of a pointer object or to the name of an array object. In both cases, the operand is an expression of pointer type; the only difference is that one happens to be a converted array expression. > If you > use an array as a "value", the value is a pointer to the first element. > This covers enough cases that it's not that hard to mislead yourself that > there's no difference between them. That's not the correct distinction. For example, if an identifier appears on the left side of an assignment, it doesn't refer to the value of the object -- but the array-to-pointer conversion still occurs (making the assignment invalid because the resulting pointer expression is not an lvalue). Understand that array expressions are usually "converted" to pointer expressions, and understand the three cases where they aren't. > It also doesn't help that C allows you to declare a function parameter as > an array even though what's actually being passed is a pointer. Presumably > this feature exists for "documentation", i.e. to distinguish a pointer to > a single value from a pointer to the first of many values. Yes, that's a separate special-case rule, and yes, it causes a fair amount of confusion. > The equivalence doesn't apply to any other variable declaration (extern, > "static" global, global with external linkage, "static" local, automatic > local), because in those cases you can declare variables which are > actually arrays, whereas function parameters can't be arrays. -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"