
passing array of pointers to function
Quote:
> #include <stdlib.h>
> #include <stdio.h>
> char *n1 = "fred";
> char *n2 = "barney";
> int
> getlist(void ***list)
`void' here is bogus. You should use `char'.
Quote:
> {
> *list[0] = n1;
This is equivalent to *(list[0]), which is equivalent to **(list
+ 0). Okay, but abstractionally challenged.
Quote:
> *list[1] = n2;
This is equivalent to *(list[1]), which is equivalent to **(list
+ 1). *BOOM*
You mean (*list)[1], which is equivalent to *((*list) + 1).
Quote:
> return 2;
> }
> int
> main()
> {
> int n = 2;
> char **names = malloc(sizeof *names * n);
> n = getlist((void ***)&names);
Drop the cast.
Quote:
> printf("%s\n", names[0]);
> printf("%s\n", names[1]);
> return 0;
> }
By the way, there's no need for the triple pointer here. You
could do it just as well, and with less confusion, with a double
pointer. You only need the triple pointer if you're going to
include something like
*names = malloc (sizeof *names * n);
within getlist() itself.
--
"You call this a *C* question? What the hell are you smoking?" --Kaz