Re: double pointer
- From: Chris Torek <nospam@xxxxxxxxx>
- Date: 27 Sep 2006 19:51:47 GMT
In article <1159284121.481698.125000@xxxxxxxxxxxxxxxxxxxxxxxxxxx>
xdevel <xdevel2000@xxxxxxxxx> wrote:
Hi, if I have:
int a=100, b = 200, c = 300;
int *a[] = {&a, &b, &c};
This reminds me of a party I went to once, where everyone was named
Chris. I don't remember who I met there... :-)
Anyway, besides all the other verbiage that has occurred in this
thread, you may find some picture-based examples interesting.
See <http://web.torek.net/torek/c/pa.html>. Unfortunately I do
not include a pointer-to-pointer in the above, but after looking
at that, consider the following code fragment and ASCII-art diagram:
int a1 = 1, a2 = 2, a3 = 3;
int arr[] = { 1, 2, 3 };
int *q[] = { &a1, &arr[1] };
int **pp = &q[1];
a1 arr a2
+---+ +---+---+---+ +---+
| 1 | | 1 | 2 | 3 | | 2 |
+---+ +---+---+---+ +---+
^ ^
| /
\ / a3
| | +---+
+---|---+---|---+ | 3 |
| * | * | +---+
+-------+-------+
q ^ pp
| +-------+
\---------* |
+-------+
Here, "pp" is a pointer to a pointer to an int. It currently points
to a pointer to an int (the one named q[1]), which in turn points
to an int (the one named arr[1]).
The "boxes" actually exist in memory at run-time, somewhere in your
C system, or at least the C system is required to act as if they do.
The arrows are the values of the pointers; those also exist. The
labels -- a1, arr, a2, and so on -- need not exist at all at run-time,
as long as the compiler can somehow arrange for:
q[0] = &a3;
to change the arrow stored in q[0] so that it points to a3.
If we store NULL in "pp", it points nowhere, which is hard to
draw in ASCII; those with an EE background (or experience) might
draw it as "grounded:
+-------+
| * |
+---|---+
|
_|_
\ /
It is impossible, however, to set "q" to NULL; q is merely a label
that covers the two actual objects q[0] and q[1]. We can set q[0]
or q[1] to NULL, because those are labels for actual boxes, containing
arrows (pointers). But we can ask the C compiler to "store q" into
pp:
pp = q; /* same meaning as "pp = &q[0];" */
because the compiler sees a request for "the value of q" as a way
for you to ask for the address of the object q[0]. That is, the
"value" of an array object is the address of the array's first
element. (This only happens "one at a time"; see the pa.html page
above, again.)
Pictorially speaking, you can think of this as: 1. All objects
are "boxes containing values" (the "value" of an automatic object
is just trash until initialized of course). 2. Pointer *values*
are "arrows", which can be stored in boxes of the appropriate type,
and which then point at some other object -- a box -- that can
contain another value. An array, however, is just a collection of
one or more boxes. The "real" value of the array is "every value
stored in every box", but C compilers will not let you get them
all in one lump, as it were; if you ask for the value of the array,
you get the address of the first box. 3. Setting any particular
pointer object to NULL "grounds" the pointer, so that it no longer
points to any box.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
.
- References:
- double pointer
- From: xdevel
- double pointer
- Prev by Date: Re: C99 Versus ANSI.
- Next by Date: Re: C99 Versus ANSI.
- Previous by thread: Re: double pointer
- Next by thread: help me in this prg
- Index(es):
Relevant Pages
|