[comp.lang.c] char **x, ***x

chin@ankh.ftl.fl.us (Albert Chin) (06/05/90)

Pardon the stupidity but what do the following mean:

char **x;
char ***x;

thanks,

albert chin ... mthvax!mamia!albert
  

smf@cup.portal.com (Steven Murray Field) (06/06/90)

Lets start, as always from the inside-out.  The declarations:
>
> char **x;
>
describes a variable 'x' that will 'point' to a value that will then
finaly point to a 'char'.

The other way to look at it is, 'X is a pointer to a pointer to char.'

The next declaration:
>
> char ***x;
>
Just adds one more level of indirection, as long as you are familiar with
pointers in general. Thus we could look at a full list and their meanings:

char x;          X contains char
char *x;         X contains pointer to char  x->c
char **x;        X contains pointer to a pointer to char  x->->c
char *...*x;     X contains pointer to ... to char   x->...->c

On the Macintosh, (and here I may confuse you), the use of ** is called
a 'handle'. This is because on the MAC, when you request a block of
memory, you could get just a pointer to it and it remains until you free
it.  In addition, to facilitate Mac's memory management, there are 2 kinds
of allocated memory, MASTERS and BLOCKS. Masters are always garaunteed to
be at fixed locations, and Blocks may move around.  Thus in this scheme
when you request a block the block is allocated from free-space and the
address, (first pointer) is placed in a MASTER. The address (second pointer)
of the MASTER is then returned to you.  When MAC moves memory around,
it only has to update the MASTER. YOU can have as many copies of the 
pointer to the MASTER as you want and so any memory movement is
transparent. 

We can se than, in this example, that

char **x, becomes : x is a handle of char or  x->MASTER->Block_of_char

--------

The above is one elegant, IMHO, use of ** indirection.  Another use may
be a structure that has pointers to kinds of itself, or generic pointers
to other structures and access is simplified in that way.  That goes
for use of *...*, (many).
Steve