Problem with use of 'void **'

D'Arcy J.M. Cain darcy at druid.uucp
Mon May 28 09:43:01 AEST 1990


In article <1990May26.203219.10343 at athena.mit.edu>
Ken Raeburn <Raeburn at MIT.Edu> writes:
>
>In article <1990May25.012342.10144 at csis.dit.csiro.au>
>peterf at csis.dit.csiro.au (Peter A Fletcher) writes:
>
>|> >void problem(void **a, int l)
>|> >{
>|> >    *a = malloc(l);
>|> >}
>|> >typedef char fiftychars[50];
>|> >int main(int argc, char *argv[])
>|> >{
>|> >    fiftychars *a;
>|> >    problem(&a, 50);
>
>|> >void.c:18: warning: argument passing between incompatible pointer types
>
>In article <1990May26.011714.7624 at druid.uucp>, darcy at druid.uucp (D'Arcy
>J.M. Cain) confuses the issue by writing:
>
>|> 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.
>
>... and continues to get more confused from there.
OK, I assumed that "fiftychars *a" was equivalent to "char *a[50]" since
"fiftychars a" would be the same as "char a[50]."  This would have made
a an array of 50 pointers to char.  That means that a would be a pointer
to a character array.  So then I tried to think of what else it could mean.
It didn't seem reasonable that it could mean that a was a pointer to an
array of 50 characters unless the 50 characters already existed in which
case why malloc them?  At that point I wrote a little test program and
studied the assembler output to see if I could glean the meaning.

Well, according to AT&T, Borland and GNU, the value of a is the same as *a.
A little thought shows why this is true.  The declaration creates an array
of 50 characters and creates a pointer to them which is what a is.  Since
a is a pointer, it can be assigned to which is what happens when problem
is called.  At this point the array that was originally created is still
in memory but is inaccessible to the program.  I can't help but feel that
this is not was the original poster had in mind.

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