Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site vaxwaller.UUCP Path: utzoo!watmath!clyde!burl!ulysses!mhuxr!ihnp4!zehntel!varian!vaxwaller!karen From: karen@vaxwaller.UUCP Newsgroups: net.lang.c Subject: Re: C declarations (with examples) Message-ID: <207@vaxwaller.UUCP> Date: Tue, 5-Feb-85 16:55:55 EST Article-I.D.: vaxwalle.207 Posted: Tue Feb 5 16:55:55 1985 Date-Received: Sat, 9-Feb-85 06:46:34 EST References: <7699@brl-tgr.ARPA> <1827@uvacs.UUCP> Organization: Varian, Walnut Creek, CA Lines: 62 > > I have a question about C declarations. The [] notation is equivalent > > to the * notation, right? > >% cat > test1.c < test2.c <main() { | char x[]; > char y[]; | main() > y = "abc"; | { >} | } >EOF | EOF >% cc test1.c | % cc test2.c >"test1.c", line 3: illegal lhs of | Undefined: >assignment operator | _x > > In test1, the compiler tells us that you can't change the value of the >identifier which indicates the start of an array. No matter that they array >has no elements -- it just won't permit it. Otherwise, a programmer could >lose track of his array. In test2, the compiler assumes that the (evidently) >null array 'x' must be declared in some other load module; when it's not found, >the loader complains. >... >it won't let you risk losing all references to a block of allocated memory. >Seems like a good idea to me. > >Ray Lubinsky University of Virginia, Dept. of Computer Science it is true that you can't change the value of the identifier which indicates the start of the array, but i disagree as to why. if you compile into assembly language ("cc" with "-S" on unix) the assembly explains things well. the following example shows that: 1. the compiler doesn't care if i lose all reference to "ppp", and 2. "a" has no contents; it is only an address, whereas "p" has assignable space in addition to the data it points to. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ c code: char *p = "ppp"; char a[] = "aaa"; main () { p = a; } /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ assembly: (...) .globl _p _p: .data 2 ; p gets allocated space L18: ; for a pointer .ascii "ppp\0" ; plus what it points to .data .long L18 .data .globl _a _a: ; a is merely an address .long 0x616161 ; pointing to its data (...) /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ - karen maleski {ucbvax!zehntel, amd}!varian!karen