Path: utzoo!utgpu!water!watmath!clyde!rutgers!cmcl2!brl-adm!umd5!trantor.umd.edu!chris From: chris@trantor.umd.edu (Chris Torek) Newsgroups: comp.lang.c Subject: Re: double indirection Keywords: what use??? Message-ID: <2292@umd5.umd.edu> Date: 13 Feb 88 08:06:42 GMT References: <4292@rosevax.Rosemount.COM> Sender: ris@umd5.umd.edu Reply-To: chris@trantor.umd.edu (Chris Torek) Organization: University of Maryland, College Park Lines: 116 In article <4292@rosevax.Rosemount.COM> richw@rosevax.Rosemount.COM (Rich Wagenknecht) writes: >Could someone give a small example of a use of double indirection or >just explain when and where it's used. This sounds circular, but is precisely true: One uses double indirection where one needs two levels of indirection. Two examples: char *cmds[] = { "foo", "bar", "baz", 0 }; int cmdindex(s) char *s; { char **cmdp; for (cmdp = &cmds[0]; *cmdp != NULL, cmdp++) if (strcmp(s, *cmdp) == 0) return (cmdp - &cmds[0]); return (-1); /* not there */ } and char * word(s, nextp) char *s, **nextp; { char *first; /* skip leading spaces */ while (isspace(*s)) s++; if (!isalnum(*s)) return (NULL); /* no word here */ first = s; /* skip word characters */ while (isalnum(*s)) s++; if (*s == 0) /* no more characters */ *nextp = NULL; else { *s = 0; nextp = s + 1; } return (first); } >... More specifically I've seen this: > > char **argv. > >Now I've used char *argv[] many times but can't see how the two relate. This is a special case. IMPORTANT: There are no `array of ' types in C. None. There ARE `array of ' types, just not `array of ' types. `array of int' does not exist; `array 4 of int' does. The constant is *required*. Now then, on to parameters: There are no array parameters in C. While you can declare a parameter as `array of ', or even (belying what I said above) as `array of ', the parameter's type is NOT `array of '. It is quietly adjusted by the compiler to `pointer to '. Given main(argc, argv) int argc; char *argv[]; { the compiler *really* sees `Function main: function returning int. First parameter: argc, int. Second parameter: argv, pointer to pointer to char. Code: ...' Note that f(a) int a[][]; { is not a legal declaration, and even PCC (4.3BSD) does not accept it. This is because `array of ' is adjusted to `pointer to ', changing `array of array of int' (no s) to `pointer to array of int' (still no !). f(a) int a[][10]; { *is* legal: the declaration `array of array 10 of int' is adjusted to `pointer to array 10 of int', and now there is an in all the `array of 's. Likewise: f(a) int a[][][]; { /* illegal */ f(a) int a[][][4]; { /* illegal */ f(a) int a[][3][4]; { /* legal */ f(a) int a[2][3][4]; { /* legal */ Note that in the last case, the declaration is changed from `array 2 of array 3 of array 4 of int' to `pointer to array 3 of array 4 of int'---the `array 2 of' is forgotten entirely. You can put any arbitrary constant in the first dimension of a parameter declaration, because that constant is ignored. NONE OF THE ABOVE APPLIES TO `ordinary' VARIABLE DECLARATIONS--- ONLY PARAMETER ARRAY DECLARATIONS ARE `adjusted'. -- In-Real-Life: Chris Torek, Univ of MD Computer Science, +1 301 454 7163 (hiding out on trantor.umd.edu until mimsy is reassembled in its new home) Domain: chris@mimsy.umd.edu Path: not easily reachable