TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

C's index[arr] notation

5 pointsby leegaoabout 14 years ago
I had always assumed that it was merely some strange artifact of the language, until I finally thought about it, and then finally understood:<p>arr[index] is equivalent to <i>(arr + index), which by the commutative property of addition is equivalent to </i>(index + arr) = index[arr].<p>Mind = blown

1 comment

marcomonteiroabout 14 years ago
It's not the same as (index + arr). arr is a pointer to the beginning address of a series of particular "types". These types have a specific size in memory. Index increments the pointer by (n * size_of(type)).<p>arr[index] == arr + (n * size_of(type)) == (n * size_of(type)) + *arr
评论 #2436523 未加载
评论 #2440093 未加载
评论 #2440094 未加载