Skip to contentSkip to conversation

pointer arithmetic vs. arrays

DISCUSSION James Colannino 2 participants 2 messages Feb 26, 2006

lore
Share & tools
lore
#0 lore raw

Hey everyone. I've heard (and have also read in "The C Programming Language") that pointer arithmetic in place of arrays is, in general, faster. However, I've also heard that modern compilers automatically make optimizations that account for this difference.

In everyone's opinion, taking both readability and issues of speed into account, what is better to use? I'd imagine this would depend on the situation. Just trying to pick up better coding habits as I learn :)

James

Signature · 4 lines
My blog: http://www.crazydrclaw.com/
My homepage: http://james.colannino.org/

"If Carpenters made houses the way programmers design programs, the first woodpecker to come along would destroy all of civilization." --Computer Proverb
#1 lore raw
Quoted 8 lines · James Colannino

Hey everyone. I've heard (and have also read in "The C Programming Language") that pointer arithmetic in place of arrays is, in general, faster. However, I've also heard that modern compilers automatically make optimizations that account for this difference.

In everyone's opinion, taking both readability and issues of speed into account, what is better to use? I'd imagine this would depend on the situation. Just trying to pick up better coding habits as I learn :)

In most cases, I'd recommend array indexing.

If something can reasonably be expressed in either form, array indexing is usually clearer (both to read and to debug), and compilers which don't optimise array access are rare enough not to be worth worrying about.

Signature · 1 line
Glynn Clements <[email protected]>