Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: extern array as a pointer |
| Date | 2016-07-12 13:33 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <87mvlnyr13.fsf@bsb.me.uk> (permalink) |
| References | <27db6ff2-216f-41c6-a9e0-c06d206f5262@googlegroups.com> <nm2ho6$455$1@dont-email.me> <20160712135839.349cd1a4@archvm> |
Frank Tetzel <s1445051@mail.zih.tu-dresden.de> writes:
>> > why does the following program crash at line# 20.
>> > Is it even legal to define array as an integer
>> > pointer while infact it's original definition is
>> > single dimensional array or five elements.
>> >
>> > 1 #include "stdio.h"
>> > 2
>> > 3 int foo();
>> > 4
>> > 5 int array[5];
>> > 6
>> > 7 int main()
>> > 8 {
>> > 9 foo();
>> > 10 printf("array[0] is %u", array[0]);
>> > 11 return(0);
>> > 12 }
>> > 13
>> > 14 foo.c:
>> > 15
>> > 16 extern int *array;
>> > 17
>> > 18 int foo()
>> > 19 {
>> > 20 arr[0]= 10;
>>
>> Did you mean array[0] here?
>>
>> > 21 return(0);
>> > 22 }
>> >
>> >
>>
>> You have a normal array in one module which is defined as a pointer
>> to array in another, assuming arr was meant to be array. Try writing:
>>
>> extern int array[];
>>
>> in foo.c
>
> But why? Why is "extern int *array" containing the first element which
> is 0? Why doesn't the compiler or rather the linker warn about this? Is
> the type information lost for the linker?
Yes, it's sadly not common for this sort of thing to be checked at link
time.
> In the past i used *array and array[] interchangeable. I might have to
> reconsider that.
They are interchangeable only when they appear as formal parameters in a
function declaration or definition and that's only because C can't pass
"naked" array values so the [] form is redundant. It was (probably
unfortunately) appropriated to mean the same thing in this context.
In other contexts, a pointer declaration and an array declaration are
very different. Think of what code the compiler must generate for
extern int *a;
...
a[0] // a names a pointer-sized object whose content must loaded
// and used in a indirect memory access.
and
extern int a[10];
...
a[0] // a names a 10-int sized object. It is the address of that
// object that must be used.
But there are all sorts of other ways to see why this distinction
matters. For example, consider what &a means in each case.
--
Ben.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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