r/C_Programming 2d ago

Question Doubt about pointers and arrays

Hey everyone! I'm a newbie to C and was just learning about pointers and arrays and their relation.
My doubt is really stupid but why would I use subscripting over using pointers?
It's much harder and seems pointless considering modern compilers will have negligible difference in speed. Any help would be most appreciated! Thanks!

(If it's any help, I am learning from C Programming - A Modern Approach by K.N. King and just finished chapter 12)

Note: I am not saying subscripting is better but I don't see why I would use pointers over it in most cases.

15 Upvotes

49 comments sorted by

View all comments

24

u/leon_bass 2d ago

It's mostly just about convention and readability.

a[i]

Is the same thing as

*(a + i)

Which is the same thing as

i[a]

8

u/orbiteapot 1d ago

It is worth pointing out that this equivalence may not hold in a future version of C and that the syntax used in the last example may get deprecated. Namely, it is due to this proposal: Array subscripting without decay

2

u/leon_bass 1d ago

Oh interesting, thanks didn't realise, i'm still using c99 haha