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


Groups > comp.lang.c > #90267

Re: extern array as a pointer

From Nobody <nobody@nowhere.invalid>
Newsgroups comp.lang.c
Subject Re: extern array as a pointer
Date 2016-07-12 13:26 +0100
Organization A noiseless patient Spider
Message-ID <pan.2016.07.12.12.26.36.123000@nowhere.invalid> (permalink)
References <27db6ff2-216f-41c6-a9e0-c06d206f5262@googlegroups.com>

Show all headers | View raw


On Tue, 12 Jul 2016 03:06:29 -0700, sinbad wrote:

> why does the following program crash at line# 20.

The program shouldn't even compile cleanly, given that you reference a
variable "arr" which is declared nowhere.

Assuming that you meant to write "array", it will typically crash because
the declaration doesn't match the definition.

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

You didn't *define* "array" as an integer pointer, you *declared* it as
such.

And, no, it won't work. Arrays aren't pointers. They *decay* to pointers
when used as expressions (as opposed to lvalues), but they aren't pointers.

If you do:

> int array[5];

the object file will contain a reservation of 5*sizeof(int) bytes and the
symbol table entry for array will reference the start of that reservation.

If you do:

> int *array;

the object file will contain a reservation of sizeof(int*) bytes and the
symbol table entry for array will reference the start of that reservation.

With an extern declaration:

> extern int array[];

an expression such as array[i] will result in code along the lines of

> *(int*)(array + i*sizeof(int))

With an extern declaration like:

> extern int *array;

an expression such as array[i] will result in code along the lines of

> *(*(int*)array + i * sizeof(int))

I.e. there will be an extra level of indirection based upon the fact that
the symbol refers to the address of a pointer to the first element rather
than to the address of the first element itself.

If you kept the existing definition but declared array as
"extern int array[];", everything should work.

If you kept the existing declaration but defined "array" as "int *array;",
then you'd generate correct code, but unless you added some more code
somewhere to initialise the pointer to point to valid memory before
you used it, you'd still get a segfault.

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