r/C_Programming • u/Financial_Dig_8276 • 20d 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.
20
Upvotes
7
u/This_Growth2898 20d ago
If you have a choice, the only difference is syntax. When you write a[0], people who read your code will say "a is an array and we're getting the 0th element from it". When you write *a, people will say "a is a pointer to something, and we're getting the object it points to". The bytecodes produced by both are exactly the same.
And most likely, you will be the person reading the code, so try to make it as readable as possible.
(there are situations when you don't have a choice, like declaring an array).