extern char *foo vs. extern char foo[]
r91400 at memqa.uucp
r91400 at memqa.uucp
Wed Jun 13 21:39:40 AEST 1990
In article <1990Jun13.030910.11593 at sharkey.cc.umich.edu%sevihc>, reso%sevihc at sharkey.cc.umich.edu (Dennis Reso) writes:
> 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"
I thought that, in this case, a[5] does not yield a pointer, but rather
the fifth element in the array. Is that not why people prefer sometimes
to create their arrays dynamically, so that a[5] WOULD return a pointer?
...something like
int *a[6],b[120];
a[0]=&b[0]; a[1]=&b[20]; a[2]=&b[40];
a[3]=&b[60]; a[4]=&b[80]; a[5]=&b[100];
Indeed, I wouldn't do it quite this way (it may not even be syntactically
correct), but it is an illustration of what I mean. This construction
does support the syntax a[5][10], and a[5] does return a pointer, and
it saves on a multiplication. Simply declaring int a[6][20] would not
do this.
So, while the illustration he gave is insightful, C doesn't do things
that way unless you tell it to.
Michael C. Grant
Motorola Inc., Austin, Texas
(Motorola does not care what I think.)
More information about the Comp.lang.c
mailing list