Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #93773

Re: Question about extern declaration

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: Question about extern declaration
Date 2016-09-19 01:23 +0100
Organization A noiseless patient Spider
Message-ID <8737kwka90.fsf@bsb.me.uk> (permalink)
References <nrn6m7$mpr$1@news.xmission.com> <nrn9ts$v7t$1@dont-email.me>

Show all headers | View raw


BartC <bc@freeuk.com> writes:

> On 19/09/2016 00:06, Kenny McCormack wrote:
>> So, I've got something in one source file like:
>>
>>     struct foo *bar[] = { &s1, &s2, 0 };
>>
>> and I want to access bar from another source file.
>>
>> So, I do:
>>
>>     extern struct foo **bar;
>>
>> but it doesn't work, until, after much hair pulling, I change it to:
>>
>>     extern struct foo *bar[];
>>
>> which is, of course, sensible, as it matches the original declaration.
>>
>> But all these years, I always assumed that, most of the time, **x and *x[]
>> pretty much meant the same thing.  I've always used "char **argv" in
>> main(), even though the book-approved syntax is char *argv[].  It has
>> always worked the same.
>>
>> So, what, really, is the difference between the two forms (specifically, in
>> an extern statement)?
>
> Outside of a parameter list:
>
>  T **bar;          // pointer to pointer to T
>  T *bar[5];        // 'value' array 5 of pointer to T
>  T (*bar)[5];      // (pointer to array 5 of T)
>
> The first two forms, which is what you have, are not the same thing.
>
> I've put in the 5 to make it clearer: the first form, declared in
> static memory, will reserve space for one pointer. The second will
> reserve space for 5 pointers.
>
> Outside of a parameter list:
  ^^^^^^^
>
>  T **bar;          // pointer to pointer to T
>  T *bar[5];        // pointer to pointer to T
>
> Here they /are/ the same thing (with the 5 being discarded), as a
> value array doesn't make sense for a parameter.

I think you meant "inside a parameter list".

<snip>
-- 
Ben.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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