Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: extern array as a pointer |
| Date | 2016-07-12 12:33 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <nm3650$g16$1@dont-email.me> (permalink) |
| References | <27db6ff2-216f-41c6-a9e0-c06d206f5262@googlegroups.com> |
On 07/12/2016 06:06 AM, sinbad wrote:
>
> Hi,
>
> 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;
> 21 return(0);
> 22 }
Other people have already given you accurate explanations, so I guess
I'll do what I do best, and give you the corresponding citations from
the standard:
"In the set of translation units and libraries that constitutes an
entire program, each declaration of a particular identifier with
external linkage denotes the same object or function." (6.2.2p2)
"All declarations that refer to the same object or function shall have
compatible type; otherwise, the behavior is undefined." (6.2.7p2)
int[5] and int* are not compatible types. One requires storage space for
5 'int' objects; the other requires storage space for a single pointer
to an 'int'.
C contains a couple of features that can cause newbies to confuse arrays
with pointers. First of all, in most contexts, an lvalue of array type
is automatically converted into a pointer to the first element of that
array. The only three exceptions are
1. "sizeof array"
This expression gives you the size of the actual array, and NOT the size
of a pointer to the first element of the array.
2. &array
This expression returns a pointer of type T(*)[N], where T is the
element type of the array, and N is the length of the array. If array
had been converted to a pointer to the first element, then &array would
have had the type T**.
3. char array[] = "string";
The string literal causes the creation of an anonymous array of char
containing {'s', 't', 'r', 'i', 'n', 'g', '\0'}. When used as an
initializer for a character array, the contents of the anonymous array
are copied into that character array. In contrast, for initialization of
all other data types, the normal conversion occurs:
char *ptr = "string";
the string literal is an lvalue of array type, and therefore gets
converted to a pointer to the first element of the array ('s'), and that
pointer is stored in ptr.
The second feature that causes this confusion has already been mentioned
by Ben Bacarisse: a declaration of a parameter of a function that looks
like it declared that parameter as an array actually declares that
parameter to be a pointer to the element type of the array. The length
specified for the array is completely ignored - it is functionally no
more significant than a comment.
These features were designed into the language because the authors of
the language thought they'd be convenient, and once you get used to
them, they are convenient. But until you get used to them, they're a
great cause of confusion.
Back to comp.lang.c | Previous | Next — Previous 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