Path: utzoo!attcan!telly!druid!darcy From: darcy@druid.uucp (D'Arcy J.M. Cain) Newsgroups: comp.lang.c Subject: Re: Problem with use of 'void **' Message-ID: <1990May27.234301.20331@druid.uucp> Date: 27 May 90 23:43:01 GMT References: <1990May25.012342.10144@csis.dit.csiro.au> <1990May26.011714.7624@druid.uucp> <1990May26.203219.10343@athena.mit.edu> Reply-To: darcy@druid.UUCP (D'Arcy J.M. Cain) Organization: D'Arcy Cain Consulting, West Hill, Ontario Lines: 51 In article <1990May26.203219.10343@athena.mit.edu> Ken Raeburn writes: > >In article <1990May25.012342.10144@csis.dit.csiro.au> >peterf@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@druid.uucp>, darcy@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@druid) | Government: D'Arcy Cain Consulting | Organized crime with an attitude West Hill, Ontario, Canada | (416) 281-6094 |