r/C_Programming • • Aug 08 '26

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

55 comments sorted by

View all comments

28

u/leon_bass Aug 08 '26

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]

60

u/YouSayItLikeItsBad Aug 08 '26

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

5

u/SolarLiner Aug 08 '26 edited Aug 12 '26

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

EDIT: override -> overload

1

u/mikeblas Aug 08 '26

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

3

u/Woshiwuja Aug 08 '26

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

1

u/mikeblas Aug 08 '26

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

3

u/DrShocker Aug 08 '26

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

2

u/SolarLiner Aug 12 '26

lol that is correct, my brain came up with the wrong word

10

u/orbiteapot Aug 08 '26

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

3

u/Valuable_Leopard_799 Aug 08 '26

I was of the impression that this was proposed and thrown out many many times. I've read blogposts from 17 years ago from people proposing this.

(Not sarcasm, genuinely asking) Is there anything different about now?

5

u/glasket_ Aug 09 '26

The current committee is far more open to making changes than 17 years ago. The proposal was already integrated last year.

2

u/leon_bass Aug 08 '26

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

4

u/smallproton Aug 08 '26

Waitaminute, please!

I consider myself a rather knowledgeable C programmer, but I don't think I've ever read about your #3.

And I actually can't understand how i[a]==a[i]. Can you explain this to me, please?

8

u/CarnivorousGoose Aug 08 '26

It’s because of the equivalence of the first and second. Thus, i[a] = \*(i + a) = \*(a + i) = a[i]

4

u/smallproton Aug 08 '26

Suddenly it makes a lot of sense!

Thanks a bunch!

4

u/leon_bass Aug 08 '26

I believe the C standard defines indexing as the following

a[i]  ==  *(a + i) 

And then since addition is commutative

a[i]  ==  *(a + i)  ==  *(i + a)  ==  i[a]

But then you might wonder but what if i is a different type to the elementsize of a, how do they add together?

So the compiler will make the offset to the array be in terms of the array element size so the final address will always be a + i * sizeof(a element type)

So if a was an array of 8 byte ints then i will offset in increments of 8 bytes. The compiler basically enforces that even if i comes first that we still offset by the array element type.

Some examples:

4["Hello"] == "o";

uint16_t arr[5] = {1, 2, 3 ,4, 5};
2[arr] == 3;

Hope this helps

1

u/smallproton Aug 08 '26

Thanks, that helped me a lot!

1

u/Financial_Dig_8276 Aug 08 '26

ah I see, much appreciated!

0

u/[deleted] Aug 08 '26

[deleted] — view deleted comment

4

u/megalogwiff Aug 08 '26

nope. please don't advise others if you don't know C

3

u/ThatIsATastyBurger12 Aug 08 '26

Your comment is incorrect, the three are always guaranteed to be the same

-4

u/iOSCaleb Aug 08 '26

Keep in mind that the i in `a[i]` and the i in`*(a + i)` are not the same if the size of the underlying type of the array is greater than 1. That is, if you have an array of some type that’s e.g. 4 bytes long, then `a[i]` is really equivalent to `*(a + 4*i)`.

9

u/Cathierino Aug 08 '26

That's not true. Pointer arithmetic automatically takes size into account. *(a + i) is in fact equivalent to a[i] regardless of the underlying size of a's type. It could be a 1 GB sized struct and it would work all the same.

-4

u/iOSCaleb Aug 08 '26

I agree that pointer arithmetic takes care of it, but that just makes the point: the i means different things in the two expressions.

→ More replies (1)