extern char *foo vs. extern char foo[]

Dennis Reso reso%sevihc at sharkey.cc.umich.edu
Wed Jun 13 13:09:10 AEST 1990


I found it much easier to deal with pointers and "arrays" when I
stopped looking at "[]" as meaning "array", and treated it like
the postfix "offset from pointer" operator. Thus the notation
"int a[5][10]" rather than "int a[5,10]", where "[10]" operates
on the expression "a[5]" which yeilds a pointer. Think of it as
just another shorthand notation:

                "a[5][10]"      == "*(*(a+5) + 10)"
            as  "record->field" == "(*record).field"

The confusion is not from mishandling of "arrays" by not passing
their entirety in pass-by-value situations:

            int a[10];
            foo(a);

but in the use of "[10]" to inform the compiler to set aside
storage in the first place.  This is yet another shorthand
notatation that perpetuates the concept of "arrays" as they
exist in other languages.  Don't get me wrong...I appreciate
the existance of this shorthand. I would hate to declare
pointers and malloc() everything.


The reason you can't take the address of a pointer as in
"foo(&a)"  (or say "a++"), is because due to its declaration,
you did not request a storage location to hold the address as
in "int *a;".  The compiler is then free to replace all
occurrences of "a" with *expressions* relative to the stack
pointer for automatic variables, or the beginning of global 
storage for globals.  (I wouldn't say "&(3+5)", either - do
some compilers do something useful with this?)

Separate the concepts of data storage and the storage of a
pointer to data storage and the confusion disappears.

________________________________________________________________________
Dennis Reso
At home in Ypsilanti, MI USA       {sharkey|itivax}!sevihc!reso
Ford Motor Company, Dearborn       reso at pms415.ford.com  [128.5.220.115]



More information about the Comp.lang.c mailing list