extern int table[] vs extern int *table; 
Author Message
 extern int table[] vs extern int *table;

In view of the close connection between arrays and pointers, it might seem
resonable to replace a declaration like

        extern int table[];
with
        extern int *table;

BUT ... this is INCORRECT, why???

Please write me back if you know the answer ... thanks :)



Fri, 15 Sep 2000 03:00:00 GMT  
 extern int table[] vs extern int *table;

# In view of the close connection between arrays and pointers, it might seem
# resonable to replace a declaration like
#         extern int table[];
# with
#         extern int *table;
# BUT ... this is INCORRECT, why???

The short answer: because the latter gets at table[i] with two
indirections (symbol table lookup for table, then index into
the table) and the former with only one (index right away because
array addresses are guaranteed to be constant).

Missing out or doing one indirection too much certainly does not
do what you expect. So don't do that. Arrays are not pointers.

The long answer is in Peter van der Linden's "Expert C Programming".

Regards,

        Jens
--
Jens Schweikhardt  http://www.shuttle.de/schweikh
SIGSIG -- signature too long (core dumped)



Sat, 16 Sep 2000 03:00:00 GMT  
 extern int table[] vs extern int *table;

Hi

See C-faq 6.

Quote:

> In view of the close connection between arrays and pointers, it might seem
> resonable to replace a declaration like

>         extern int table[];
> with
>         extern int *table;

> BUT ... this is INCORRECT, why???

> Please write me back if you know the answer ... thanks :)

--
/***************************************
      Masahiro Kondo (Tokyo Japan)

***************************************/


Sat, 16 Sep 2000 03:00:00 GMT  
 extern int table[] vs extern int *table;



Quote:
>In view of the close connection between arrays and pointers, it might seem
>resonable to replace a declaration like

>        extern int table[];
>with
>        extern int *table;

>BUT ... this is INCORRECT, why???

Because it is the usage of arrays and pointers (in expressions) that is
closely linked, not the way they are stored. Trying to interpret the
contents (i.e. the bits that make it up) of an array as if they represented
a pointer would simply result in nonsense. Also there's no reason why a
C compiler can't perform a degree of type checking between translation units
so this might not even compile successfully (and if they are in the same
translation unit the compiler will certainly complain). The bottom line here
is that arrays and pointers are not compatible types.

--
-----------------------------------------


-----------------------------------------



Sat, 16 Sep 2000 03:00:00 GMT  
 extern int table[] vs extern int *table;

Quote:

> In view of the close connection between arrays and pointers, it might seem
> resonable to replace a declaration like
>         extern int table[];
> with
>         extern int *table;
> BUT ... this is INCORRECT, why???

Hi Keith,

The FAQ for this newsgroup contains an *excellent* explanation of
the differences and similarities between pointers and arrays. It
is very well written and easy to understand. Amongst other things
it answers your question very well. These are just *some* of the
questions answred in the FAQ:

6.1:    I had the definition char a[6] in one source file, and in
        another I declared extern char *a.  Why didn't it work?
6.2:    But I heard that char a[] was identical to char *a.
6.3:    So what is meant by the "equivalence of pointers and arrays" in
        C?
6.4:    Then why are array and pointer declarations interchangeable as
        function formal parameters?
etc. ...

You can get the FAQ at http://www.eskimo.com/~scs/C-faq/top.html or
at ftp://rtfm.mit.edu/pub/usenet/comp.lang.c/C-FAQ-list and it gets
posted to this newsgroup and to news.answers regularly (at the
beginning of each month).

Stephan
(initiator of the campaign against grumpiness in c.l.c)



Sat, 16 Sep 2000 03:00:00 GMT  
 extern int table[] vs extern int *table;

Quote:

> Hi

> See C-faq 6.

Or see the IAQ chapter *5* (Yep, the IAQ is off by one). It contains
answers to questions like:
5.1:    I had the definition char a[6] in one source file, and in
        another I declared extern char a[].  Why did it work?
5.2:    But I heard that char a[] was different from char a[6].
5.3:    So what is meant by the ``equivalence of pointers and arrays''
        in C?
5.4:    Then why are array and pointer declarations interchangeable as
        function formal parameters?

You can get the comp.lang.c IAQ at:
   http://www.plethora.net/~seebs/faqs/c-iaq
because it is (not very surprisingly) posted only infrequently

Stephan
(initiator of the campaign against grumpiness in c.l.c)



Sun, 17 Sep 2000 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Why is extern int * buf != extern int buf[] ?

2. extern int foo(int); vs int foo(int);

3. extern var vs. static extern var

4. extern char *foo vs. extern char foo[]

5. extern int a[];

6. integer literals and extern const int problem

7. extern const int = a non constant?!?

8. main() vs int main() vs int main(void)

9. Conversion of signed long int to n-bit signed int and vs vs

10. int func(int) versus int func(int *) efficacy.

11. extern int f(); VS int f();

12. difference between extern and extern "C"

 

 
Powered by phpBB® Forum Software