Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!elroy.jpl.nasa.gov!usc!wuarchive!udel!haven!adm!smoke!gwyn From: gwyn@smoke.brl.mil (Doug Gwyn) Newsgroups: comp.lang.c Subject: Re: Array question Message-ID: <15209@smoke.brl.mil> Date: 15 Feb 91 18:52:09 GMT References: <7060009@hpfcso.FC.HP.COM> Organization: U.S. Army Ballistic Research Laboratory, APG, MD. Lines: 26 In article <7060009@hpfcso.FC.HP.COM> pgt@hpfcso.FC.HP.COM (Paul G. Tobin) writes: >It would seem that today's compilers probably somewhat close the speed >gap between array and pointer addressing, but I'd contend that they >*can* be freely interchanged. It all depends on your personal style. >Use whatever mode seems more suited to the task at hand. No, it is important to understand that pointers and arrays are NOT freely interchangeable, in order to avoid programming errors. In most (but not all) expression contexts, the name of an array is converted by the compiler to a pointer to the first element of the array, and that pointer is used in the subsequent evaluation. An important exception is when the array name is the operand of the "sizeof" operator; in such a situation the result of evaluation the "sizeof" unary expression is the number of bytes in the entire array, not the number of bytes in a pointer to an element of the array (the latter is typically always 4, in many implementations). The exact rules are spelled out in the C standard, and in A.7.1 in the second edition of K&R these rules are summarized. In the first edition these rules do not seem to have been explained, although certainly some of the exceptions were enforced by existing compilers used as a basis for the notion of what the C language definition was at the time of preparation of the first edition of K&R. The main new feature added for ANSI C is that the & operation can be applied in a more menaingful way to an array (name) than was true for many older existing C implementations.