Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ames!haven!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c Subject: Re: Indefinite-length array as member of struct: how? Keywords: char string [] [0] [1] Message-ID: <18492@mimsy.UUCP> Date: 11 Jul 89 18:29:53 GMT References: <7360@c3pe.UUCP> <321@yetti.UUCP> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 47 In article <321@yetti.UUCP> asst-jos@yetti.UUCP (Jonathan) writes: >Remember that although by definition, the name of an array is a pointer >to the array, there are certain limitations. The name of an array is *not* a pointer to the array. Forget `array equals pointer'; it is false. In an rvalue context, an object of type `array N of T' is converted to one of type `pointer to T'; its value is the address of the 0'th (first) element of the array (&array[0]). >... namely > > char string[SIZE]; > > *string = .... > >is invalid. Apply the rule: `In an rvalue context'---do we have an rvalue context? The context here in which the object of type `array SIZE of char' appears is `*string'. Looking up the description for `*' in any good C book, we find that it means either multipy (infix) or indirect (prefix). Here it is being used as the latter. The target of the `*' (here `string') is indeed in an rvalue context. Thus, `string' is converted from an object of type `array SIZE of char' to one of type `pointer to char'. Its value is the address of `string[0]' (&string[0]). The details of the indirection operator are that it converts a pointer into the object to which that pointer points (here string[0]), and yeilds an lvalue (so that we can assign into string[0]). Thus, char string[SIZE]; *string = ; is legal and means `write the value produced by the integer expression into string[0]'. Arrays are not pointers; pointers are not arrays. There is a symmetry between the two, but they are different. The real relationship is that array object are sometimes converted into pointers, and that pointers that point to one member of an array may be used to reach any member of that array. If you want an analogy, they are like matter and energy, interconvertible (E = mc^2) but not identical (you would eat an apple, but you would not eat 7600000 gigajoules of energy [~3 oz]). -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163) Domain: chris@mimsy.umd.edu Path: uunet!mimsy!chris