Global and Extern Pointers, Arrays

Rahul Dhesi dhesi%cirrusl at oliveb.ATC.olivetti.com
Fri May 24 08:40:01 AEST 1991


In <AJ3U.91May22202642 at agate.cs.virginia.edu>
aj3u at agate.cs.virginia.edu (Asim Jalis) askes about a bug caused by the
difference between the following:

>	extern *px	/* boinks */
>	extern px[]	/* works */

Asim's question is a VVFAQ (very very frequently asked question).  In
my early C days I asked it often.  Then I wrote an answer.  Here it
is.

Date:    12 Dec 90 03:27:15 GMT
From:    dhesi%cirrusl at oliveb.ATC.olivetti.com (Rahul Dhesi)
Newsgroups: comp.lang.c
Subject: Re: confusion with char *a and char a[NUM]
References: <7656 at umd5.umd.edu> <1990Dec6.055844.5333 at clear.com>
Sender:  cirrusl!news
Distribution: na
Organization: Cirrus Logic Inc.

Let me give you a different way of understanding the difference between
char *A and char B[NUM].

After you have used malloc to assign NUM characters of storage for A,
both A and B can be used in a very similar way.  In some contexts any
reference to A or B is treated as the use of a pointer.

However, B is not a pointer.  It's just treated like one sometimes.  It
is a SYMBOLIC NAME FOR AN ADDRESS.  (And remember that a pointer is not
an address; it's a way of finding an address.)  In place of B you could
(if the language allowed it) use the absolute value of the address of
the array you call B.  You can pretend that the instant you use B, the
compiler converts it to the address for which B is a symbolic name.

A is not the same thing.  It is a VARIABLE, not an address.  The
variable is at some address, but when you use A, you usually
dereference that variable, and reach some other address.  This is why A
really *is* a pointer variable, while B is merely *treated* like one.

So, what happens when you do something like "A[3] = 7" or "B[3] = 7"?
In the first case, the compiler generates code to look at the CONTENTS
of the variable A, get those contents and add 3 to them, and treat the
result as an address.  B, however, is a symbolic name for an address.
The compiler generates code to directly take that address, add 3 to it,
and get another address.  In both cases, after the address is obtained,
a value of 7 is stored at that address.  (The above description is not
strictly accurate if A or B is on the stack.  For this description,
let's assume they are statically allocated and not in the stack.)

The terms "pointer" and "address" are often erroneously used with the
same meaning.  They are not the same.  An address is a location.  A
pointer is a way of getting to a location.  Think of the pointer as an
arrow pointing somewhere, and an address as the place the arrow is
pointing to.  A, the pointer, is an arrow, and it points to the address
where malloc() gave us memory.  B, the address, doesn't point anywhere
-- it *is* already an address.  A (a pointer) could point to B (an
address).  But B (an address) can never point to A, because B is not a
pointer.
--
Rahul Dhesi <dhesi at cirrus.COM>
UUCP:  oliveb!cirrusl!dhesi



More information about the Comp.lang.c mailing list