Path: utzoo!mnetor!uunet!mcvax!ukc!stc!datlog!dlhpedg!cl From: cl@dlhpedg.co.uk (Charles Lambert) Newsgroups: comp.lang.c Subject: Re: Address of array Message-ID: <329@dlhpedg.co.uk> Date: 8 Dec 87 13:30:45 GMT References: <126@citcom.UUCP> <163@mccc.UUCP> <422@xyzzy.UUCP> Sender: news@dlhpedg.co.uk Reply-To: cl@.co.uk (Charles Lambert) Organization: FSG@Data Logic Ltd, Queens House, Greenhill Way, Harrow, London. Lines: 56 In article <422@xyzzy.UUCP> throopw@xyzzy.UUCP (Wayne A. Throop) writes: >> pjh@mccc.UUCP (Peter J. Holsberg) >> OK - perhaps you had better tell us neophytes what you mean by the >> address of an array! > >Same as address of anything else. It is an address which, when >indirected, yields an array, and when "N" is added to it, yields the >address of an array which is itself a member of an array "N" elements >away from the array yielded by an indirection. > > [ several abstruse observations ] > >What could be simpler? Well, several other forms of explanation, I guess. This one confused me, and I *understand* the address of an array. (Just teasing) To put it another way.... Any object, of any type (integer, structure, array, etc.), has an address. Usually, if it is an object that occupies several words of memory, it is the address at which it begins. (Compiler theorists may be itching to tell me it might mean something else entirely; let's keep this simple.) The address of an object is the compiler's handle for manipulating it. You think of an object by its name; the compiler "thinks" of it by its address. The "address of an array" is the address that the compiler uses to access that array and to calculate the position of any element in the array. In C, the address of an array is the same as the address of its first element (array[0]). If you want to set up a pointer to the array, you get its address simply by naming it. Hence: pa = array; /* pa now contains the address of "array" */ which is exactly the same as pa = &array[0]; /* "&" means "address of", so pa contains the address of element [0] of "array" */ Now this is a slight quirk in C - the name of the array being a synonym for its address; for any other object (notably a struct) that is not true. If you want the address of a structure you must write ps = &mystruct; /* NOT ps = mystruct */ So we get back to the discussion from whence we came: why can't we be consistent and get the address of an array by pa = &array; ? To which the answer is: you can, with some compilers. [Further reading: The C Programming Language; Kernighan & Ritchie; pp.93-95] -------------------------- Charles Lambert