Re: double pointer



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.
.



Relevant Pages

  • Re: Need help to port VAX code to Alpha and to Itaninum
    ... Not really, the original code was wrong, and the compiler was not ... pointer to the start of the array. ... So you are trying to pass a pointer to a pointer to an array where you ... Also start looking at where you can add the "const" modifier to function ...
    (comp.os.vms)
  • Re: decrement past beginning is valid?
    ... > What I meant by legal is that a compiler will compile it. ... > that an array is the same as a pointer. ... behave the same way on all platforms. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: gdb not catching out-of-bounds pointer
    ... is also not defined by the C-standard, IOW: writing code in anything ... provided the library writer knows what the compiler writer guarantees ... etc.) that don't point into the same array than I would about the sort ... of pointer aliasing issue that started this sub-thread. ...
    (comp.unix.programmer)
  • Re: null terminated strings
    ... pointer + 1 will point to the next element in the array. ... What I don't understand is why such a thing was included in any language that's more than an assembler. ... of structures that are not 'natural' the compiler need to generate ADD instructions using sizeof. ...
    (comp.os.vms)
  • Re: lockless file descriptor lookup
    ... It isn't clear whether you want to refresh the fd_ofiles pointer to the ... the array, or the fd'th element. ... This is actually intended to catch cases where the descriptor array has expanded and the pointer to fd_ofiles has changed, or the file has been closed and the pointer at the fd'th element has changed. ... I'm attempting to force the compiler to reload the fd_ofiles array pointer from the fdp structure. ...
    (freebsd-arch)