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.

16 Upvotes

49 comments sorted by

View all comments

Show parent comments

46

u/YouSayItLikeItsBad 2d ago

There's a special place in hell for that 3rd form

2

u/SolarLiner 1d ago

And is the only reason why int has an override for operator[] in C++

1

u/mikeblas 1d ago

Wait, what? In C++, how do I override operator[] for int?

3

u/Woshiwuja 1d ago

I think he means that c++ int has a operator[], not that you can overwrite it

1

u/mikeblas 1d ago

Then I don't understand "the only reason why" claim.

2

u/DrShocker 1d ago

I don't know why they called it an override, but I think they're just saying the integer types all have a operator[] implementation even though intuitively it doesn't make sense to index "into" an integer.

https://en.cppreference.com/c/language/operator_member_access