From torek@elf.ee.lbl.gov Sun Sep 29 00:12:51 1991 From: torek@elf.ee.lbl.gov (Chris Torek) Newsgroups: comp.lang.c,comp.unix.questions Subject: Re: cc error output Date: 28 Sep 91 20:36:53 GMT Reply-To: torek@elf.ee.lbl.gov (Chris Torek) Organization: Lawrence Berkeley Laboratory, Berkeley X-Local-Date: Sat, 28 Sep 91 13:36:53 PDT In article <1991Sep27.150314.6422@hellgate.utah.edu> jwindley@cs.utah.edu (Jay Windley) writes: >If you mean "bad lhs," then it is indicating that the left side of an >assignment statement is improper, This is correct ... >such as the following: > >int x[X_SIZE]; >int y; >x = &y; and this is indeed improper ... >x indeed has the correct type -- "pointer to int," but its value is constant >and cannot be reassigned. but x is *not* constant, and x does *not* have type `pointer to int'. x has type `array X_SIZE of int'. Pick up almost any C book on the shelf of your local bookstore, turn to the chapter on `pointers and arrays', and you will probably find a flat statement---not just a hint or a `temporary concept' to be cleared up later---claiming that the name of an array is a pointer to the first element of the array. This statement is false; all of these books are wrong. The name of the array is not a pointer; the name of the array *becomes* a pointer *in a value context*. This is easily demonstrated: printf("sizeof array = %ld, sizeof pointer = %ld\n", (long)sizeof(x), (long)sizeof(int *)); As X_SIZE varies, the first number also varies, while the second number remains constant. If x were `pointer to int', it would have the same size as `int *'; it does not, so it is not. Note that the left hand side of an assigment operator is not a value context---it is an object context---so the array `x' remains an array. It is illegal to have an array object on the left hand side of an assignment operator. The next time you are in a bookstore, look at the C books and count the number that get this right. There will be very few, or even none. Chances are that the authors *believe* that an array is a pointer, and that sizeof and multi-dimensional arrays are special cases. This is backwards: *values* are special cases, and arrays are otherwise arrays. (Function parameters are also special cases. Both special cases occur very often, but they are still special.) -- In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 510 486 5427) Berkeley, CA Domain: torek@ee.lbl.gov