Problem with use of 'void **'

D'Arcy J.M. Cain darcy at druid.uucp
Sat May 26 11:17:14 AEST 1990


In article <1990May25.012342.10144 at csis.dit.csiro.au> peterf at csis.dit.csiro.au (Peter A Fletcher) writes:
>
>I would like to be able to write a routine which can pass
>a block of memory back to the caller, so the obvious way
>to do this is by passing a pointer to a pointer of any
>type - 'void **' seems the natural way to do this (using
>'void *' would allow passing addresses of non-pointers,
>which is a definite no-no).
>
Doesn't seem natural at all.

>Here's an example:
>
>#include <stddef.h>
>#include <stdlib.h>
>
>void problem(void **a, int l)
>{
>    *a = malloc(l);
>}
>typedef char fiftychars[50];
>int main(int argc, char *argv[])
>{
>    fiftychars *a;
>    problem(&a, 50);
>    return 0;
>}
>void.c: In function main:
>void.c:18: warning: argument passing between incompatible pointer types

First of all the typedef says that an array of fifty character is created
when fiftychars is declared.  However, you declare a to be a *pointer* to
this array of 50 chars.  In effect your declaration has become:
    char	**a;
since the 50 characters have not actually been allocated.  Note that you
*must* have the extra size parameter for problem.

Since a is char **, &a is char ***.  That is why the type mismatch.  The
call should be "problem(a, 50);"  However the whole code seems clumsy
aside from all that.  I would code something like the following.  Note
that I am assuming that problem is not as trivial as your stripped down
example indicates so I have not reduced the call to problem to a call to
malloc.

#include <alloc.h>
void *problem(int l)
{
    return(malloc(l));
}
int main(int argc, char *argv[])
{
    char *a;
	a = problem(50);
    return 0;
}

Followups to comp.lang.c

-- 
D'Arcy J.M. Cain (darcy at druid)     |   Government:
D'Arcy Cain Consulting             |   Organized crime with an attitude
West Hill, Ontario, Canada         |
(416) 281-6094                     |



More information about the Comp.lang.c mailing list