Xref: utzoo gnu.gcc:1635 comp.lang.c:29101 Path: utzoo!attcan!uunet!clyde.concordia.ca!news-server.csri.toronto.edu!torsqnt!hybrid!robohack!druid!darcy From: darcy@druid.uucp (D'Arcy J.M. Cain) Newsgroups: gnu.gcc,comp.lang.c Subject: Re: Problem with use of 'void **' Message-ID: <1990May26.011714.7624@druid.uucp> Date: 26 May 90 01:17:14 GMT References: <1990May25.012342.10144@csis.dit.csiro.au> Reply-To: darcy@druid.UUCP (D'Arcy J.M. Cain) Followup-To: comp.lang.c Organization: D'Arcy Cain Consulting, West Hill, Ontario Lines: 63 In article <1990May25.012342.10144@csis.dit.csiro.au> peterf@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 >#include > >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 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@druid) | Government: D'Arcy Cain Consulting | Organized crime with an attitude West Hill, Ontario, Canada | (416) 281-6094 |