Path: utzoo!mnetor!uunet!husc6!cmcl2!brl-adm!umd5!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c Subject: Re: Address of Array Message-ID: <9735@mimsy.UUCP> Date: 12 Dec 87 12:29:22 GMT References: <126@citcom.UUCP> <2550034@hpisod2.HP.COM> <1854@haddock.ISC.COM> <1442@houdi.UUCP> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 75 >In article <555@ndsuvax.UUCP> ncbauers@ndsuvax.UUCP (Michael Bauers) writes: >>The address of the array a[10] is just a. The address of the first element >>is also a (for single dimension arrays). This is already confused. >>The first element is *a. But the address of the array's pointer `The address of the array's pointer' is even more confused. >>... should be &a. In article <1442@houdi.UUCP> marty1@houdi.UUCP (M.BRILLIANT) writes: >Huh? I tried writing a C program on UNIX(tm ATT) that referenced &a, >and got the message "warning: & before array or function: ignored." >That says there's no such thing as &a. Given the declaration int a[10]; the following is true: 0. &a is currently illegal 1. &a will be legal in the near future. 2. In an expression, except as a target of sizeof, `a' and `&a[0]' have identical meanings, namely a value of type `pointer to int' that points to the first element of `a' (a[0]). 3. In an expression, `&a' will be a value of type `pointer to (int [10])' that points to the array `a'. Hence `*&a' will be a value of type `int [10]', which, in all expression contexts except as a target of sizeof, is immediately converted to a value of type `int *' (a.k.a. `pointer to int') that points to the first element of that array (here &a[0]). Thus `*&a' will mean the same thing as `a'. Hence, in the near future, one will be able to write the following: f1() { int z[4][10]; int (*p)[10]; int i, j; p = g() ? &z[0] : &z[2]; for (i = 0; i < 2; i++) for (j = 0; j < 10; j++) p[i][j] = (i + 1) * j; ... } At present, it is necessary to code this using casts (because &z[0] elicits warnings or errors about & before array), or using code like the following (with a cast version in comments): f2() { int z[4][10]; /* int (*p)[10] */ int *p; int i, j; /* p = (int (*)[10])(g() ? &z[0][0] : &z[2][0] */ p = g() ? &z[0][0] : &z[2][0]; for (i = 0; i < 2; i++) for (j = 0; j < 10; j++) /* p[i][j] = (i + 1) * j */ p[i*10 + j] = (i + 1) * j; ... } -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690) Domain: chris@mimsy.umd.edu Path: uunet!mimsy!chris