Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Question about extern declaration |
| Date | 2016-09-19 11:46 -0700 |
| Organization | None to speak of |
| Message-ID | <lnh99bwwva.fsf@kst-u.example.com> (permalink) |
| References | <nrn6m7$mpr$1@news.xmission.com> <pan.2016.09.19.12.38.38.849000@nowhere.invalid> |
Nobody <nobody@nowhere.invalid> 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 <http://www.ghoti.net/~kst>
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"
Back to comp.lang.c | Previous | Next — Previous in thread | Find similar | Unroll thread
Question about extern declaration gazelle@shell.xmission.com (Kenny McCormack) - 2016-09-18 23:06 +0000
Re: Question about extern declaration supercat@casperkitty.com - 2016-09-18 16:33 -0700
Re: Question about extern declaration BartC <bc@freeuk.com> - 2016-09-19 01:02 +0100
Re: Question about extern declaration Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-09-19 01:23 +0100
Re: Question about extern declaration BartC <bc@freeuk.com> - 2016-09-19 10:40 +0100
Re: Question about extern declaration m.labanowicz@gmail.com - 2016-09-19 03:27 -0700
Re: Question about extern declaration Nobody <nobody@nowhere.invalid> - 2016-09-19 13:38 +0100
Re: Question about extern declaration Keith Thompson <kst-u@mib.org> - 2016-09-19 11:46 -0700
csiph-web