Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!seismo!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c Subject: Re: char (*a)[] Message-ID: <8089@mimsy.UUCP> Date: Sat, 22-Aug-87 02:51:43 EDT Article-I.D.: mimsy.8089 Posted: Sat Aug 22 02:51:43 1987 Date-Received: Sun, 23-Aug-87 10:18:58 EDT References: <8942@brl-adm.ARPA> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 82 In article <8942@brl-adm.ARPA> DHowell.ElSegundo@Xerox.COM writes: >I'm confused. With good reason. >Suppose I declare: > >char (*a)[10]; >char b[10]; > >Now b is an array of size 10 of char, and a is a pointer to an array of >size 10 of char. Right so far. >So this means I should be able to say: > >a = &b; And confusion strikes. K&R C makes &b illegal. >However, as I understand it, b is actually &b[0], which means a gets set >to &&b[0], which I'm not sure makes any sense at all. This is per K&R, and compilers that implement K&R C. &b is illegal, and in PCC, is ignored with a warning, such that the compiler `sees' a = b; which is a type mismatch ( = ). Other C definitions, in particular draft ANS X3J11, make &b legal, defining it to produce an rvalue of type and having as its value the address of the entire array, whatever that means in the implementation. Given that other parts of C demand that each array be stored in a flat linear address space (that may nonetheless be disjoint from any or all other flat linear address spaces), this value will probably be the same as that of the address of the first element of b. >What exactly does a point to? This is machine- and implementation-dependent. >Does it point to the first element of an array? Quite possibly. >Does it point to a descriptor of an array? This is possible but unlikely. >How would I assign anything useful to a, if I can't use the above >type of assignment? You could write, e.g., a = (char (*)[10]) malloc(sizeof (char [10])); if (a == NULL) ... (*a)[9] = 'c'; /*or*/ strcpy(a[0], "text"); /* < 10 characters */ This does not entirely rule out an implementation that creates array descriptors, but makes things difficult for such. Note that a = (char (*)[10]) malloc(5 * sizeof (char [10])); is also legal, and creates something that can be addressed as a[4][9] = 'c'; or for (i = 0; i < 5; i++) strcpy(a[i], "text"); /* < 10 characters each */ A compiler that creates array descriptors will have quite a job dealing with these. -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690) Domain: chris@mimsy.umd.edu Path: seismo!mimsy!chris