Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!rochester!cornell!batcomputer!braner From: braner@batcomputer.tn.cornell.edu (braner) Newsgroups: comp.lang.c Subject: Re: pointers to arrays Message-ID: <1554@batcomputer.tn.cornell.edu> Date: Thu, 20-Nov-86 14:26:28 EST Article-I.D.: batcompu.1554 Posted: Thu Nov 20 14:26:28 1986 Date-Received: Fri, 21-Nov-86 05:07:21 EST References: <273@bms-at.UUCP> <1138@genrad.UUCP> Reply-To: braner@batcomputer.UUCP (braner) Organization: Theory Center, Cornell University, Ithaca NY Lines: 25 Keywords: What? Why? How? Summary: Arrays of pointers to arrays are more useful [] Pointers to arrays come in handy for efficiency in accessing 2-d arrays. See K&R about "sparse arrays". Say you have the "page" example: char page[25][80]; and you want to avoid the slow calculations needed for a direct access such as "c = page[i][j];". Set up another array of pointers to lines: char *line[25]; Initialize it once to point to the lines: for (i=0; i<25; i++) line[i] = &page[i][0]; Later you do "c = line[i][j];" - which looks the same but is MUCH faster, since no multiplication is needed to calculate &line[i][j]. (Well, it IS necessary to multiply i by sizeof(char *), but hopefully your compiler is smart enough to do that by a shift, since the size is a power of two - on most machines.) - Moshe Braner, Cornell Univ.