Article View: comp.lang.c.moderated
Article #2326Re: Pointer Arithmetic
From: Ari Lukumies
Date: Mon, 18 Mar 1996 13:42
Date: Mon, 18 Mar 1996 13:42
22 lines
1519 bytes
1519 bytes
Dan T. Abell wrote: > > This may sound like a dumb question, but can someone tell me how C > compilers do pointer arithmetic? In particular, consider an integer array > p[0], p[1], ... , p[n] > whose entry p[k] we might access using *(p + k). I imagine the typical > compiler would multiply the size of an integer by k, add the result to p, > and then report the integer located at that address. Now for my question: > Do most compilers use plain ol' multiplication or do they use bit-shift > operators. Also, does the answer depend on the type of the array p? > It is implementation dependant, but I'd guess that most compilers use multiplication since it's easier to implement. Shifts would be faster, but in many cases you would have to use a few shifts and additions to accomplish the same task. For instance, if the size of the element is 80 bytes, you would have to do shifts by 10 and 8 and add the results together, which is harder to implement in a compiler. Later, AriL -- All my opinions are mine and mine alone.
Technical Details
Message-ID:
<4ijp8i$1cf@solutions.solon.com>Path:
arc1-ca.pugleaf.net!solon.com!not-for-mailReferences:
<4i805f$47d@solutions.solon.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail From: Ari Lukumies <aril@cmt.lpr.mail.carel.fi> Newsgroups: comp.lang.c.moderated Subject: Re: Pointer Arithmetic Date: 18 Mar 1996 07:42:42 -0600 Organization: Carelcomp Products Sender: clc@solutions.solon.com Approved: clc@solutions.solon.com Message-ID: <4ijp8i$1cf@solutions.solon.com> References: <4i805f$47d@solutions.solon.com> NNTP-Posting-Host: solutions.solon.com X-Mailer: Mozilla 2.0 (WinNT; I)