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


Groups > comp.lang.c > #90269

Re: extern array as a pointer

From Richard Heathfield <rjh@cpax.org.uk>
Newsgroups comp.lang.c
Subject Re: extern array as a pointer
Date 2016-07-12 13:46 +0100
Organization Origin Sonata
Message-ID <nm2orr$sic$1@dont-email.me> (permalink)
References <27db6ff2-216f-41c6-a9e0-c06d206f5262@googlegroups.com>

Show all headers | View raw


On 12/07/16 11:06, sinbad wrote:
>
> Hi,
>
> why does the following program crash at line# 20.

Because arrays are not pointers, and pointers are not arrays.

> Is it even legal to define array as an integer
> pointer while infact it's original definition is
> single dimensional array or five elements.

It invokes undefined behaviour.

int array[5] defines an array of 5 int, with file scope and external 
linkage. If you wish to refer to this array from another source file, 
you can, by declaring it as follows:

extern int array[];

>   13
>   14 foo.c:
>   15
>   16 extern int *array;

Here, the name is right but the type is wrong. array is int[5], not int *.

C allows us to compile source files individually, linking them in a 
later step. So the compiler has to be able to compile foo.c without even 
seeing main()'s source file. It has /no idea/ that array (bad choice for 
an identifier name, by the way!) is an array. When you tell it array is 
a pointer, it believes you, and array[0] is a legal access to an object 
pointed to by an int * (you actually wrote arr[0], but I'm overlooking 
that). So there's no reason for the compiler to complain. But it's still 
wrong, and it may bite you. And, apparently, it did.

-- 
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within

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


Thread

extern array as a pointer sinbad <sinbad.sinbad@gmail.com> - 2016-07-12 03:06 -0700
  Re: extern array as a pointer BartC <bc@freeuk.com> - 2016-07-12 11:45 +0100
    Re: extern array as a pointer Frank Tetzel <s1445051@mail.zih.tu-dresden.de> - 2016-07-12 13:58 +0200
      Re: extern array as a pointer Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-07-12 13:33 +0100
      Re: extern array as a pointer Keith Thompson <kst-u@mib.org> - 2016-07-12 09:30 -0700
        Re: extern array as a pointer James Kuyper <jameskuyper@verizon.net> - 2016-07-12 12:52 -0400
          Re: extern array as a pointer Keith Thompson <kst-u@mib.org> - 2016-07-12 11:30 -0700
            Re: extern array as a pointer supercat@casperkitty.com - 2016-07-12 11:51 -0700
        Re: extern array as a pointer supercat@casperkitty.com - 2016-07-12 10:55 -0700
    Re: extern array as a pointer Keith Thompson <kst-u@mib.org> - 2016-07-12 09:28 -0700
  Re: extern array as a pointer Nobody <nobody@nowhere.invalid> - 2016-07-12 13:26 +0100
    Re: extern array as a pointer Keith Thompson <kst-u@mib.org> - 2016-07-12 09:26 -0700
  Re: extern array as a pointer Richard Heathfield <rjh@cpax.org.uk> - 2016-07-12 13:46 +0100
  Re: extern array as a pointer James Kuyper <jameskuyper@verizon.net> - 2016-07-12 12:33 -0400

csiph-web