Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!sri-unix!sri-spam!ames!aurora!labrea!decwrl!sun!gorodish!guy From: guy%gorodish@Sun.COM (Guy Harris) Newsgroups: comp.lang.c Subject: Re: Absolute addressing in Turbo-C Message-ID: <27188@sun.uucp> Date: Thu, 3-Sep-87 15:00:40 EDT Article-I.D.: sun.27188 Posted: Thu Sep 3 15:00:40 1987 Date-Received: Sat, 5-Sep-87 12:16:46 EDT References: <9140@brl-adm.ARPA> Sender: news@sun.uucp Lines: 30 > It's this: Just WHY is it in C 'better' to write: *(a+n) instead of: a[n], > where 'a' is an array of something? And how much 'better' is it? It isn't any "better"; in fact, it's exactly the same. "a[n]" is defined as "*(a+n)". The only ways in which one would be "better" would either be that one is somehow stylistically, or that one generates better code. Different people have different preferences in style, and different compilers may well not always generate better code for the same version (our compiler generates the same code for both of them), so it's not clear there is a single style that could be considered "better" in any objective sense. > I remember reading on this list some time back that this does not extend to > arrays of more than one dimension. Why? Just because the expressions will > get too complex? No, this extends quite well to arrays of more than one dimension: "a[i][j]" is (a[i])[j], and thus is *((*(a+i)) + j). There may be some confusion because "*(a+i)" is an array-valued expression, and K&R doesn't discuss this notion; however, ANSI C does - array-valued expressions, in most contexts, are converted to pointer-valued expressions whose value is a pointer to the first element of the array. This is the obvious generalization of the K&R rule that the name of an array, in most contexts, is converted to a pointer to the first member of that array; this generalization is implemented by our C compiler, and probably by most if not all other PCC-based compilers and most likely by many other compilers as well. Guy Harris {ihnp4, decvax, seismo, decwrl, ...}!sun!guy guy@sun.com