r/C_Programming • u/Financial_Dig_8276 • 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.
28
u/leon_bass Aug 08 '26
It's mostly just about convention and readability.
Is the same thing as
Which is the same thing as
10
u/iLaysChipz Aug 08 '26 edited Aug 08 '26
Because some things are only possible with pointers, like linked lists. Can't exactly use subscripting on a struct with different data types in it. For example:
4
u/Willing_Airport_9617 Aug 08 '26
You can use subscripting , no body stops you . The pointer thing is more about how it's implemented under the hood
6
u/This_Growth2898 Aug 08 '26
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).
3
u/NotStanley4330 Aug 08 '26
You need pointers for more complicated data structures like trees or linked lists. You can also have multi-dimensional arrays where each. Additionally, void * allow you to have an array with multiple different data types inside.
Pointers are the magic sauce in C.
2
u/Wertbon1789 Aug 08 '26
I think you might not quite get the relationship between them entirely (which is fine, it's a lot, just play around a bit).
Mainly, Arrays are mostly sugar for pointers, but pointers don't need to be arrays. Array variables just decay to pointers, because they're syntax sugar for pointers to multiple elements of a type, a pointer on it's own could be a pointer to one, many or no objects in memory, depends on the context and the pointers value, arrays and array members of a struct are usually expected to have one or more values, in the struct member case with the same lifetime as the struct object itself. Those expectations don't need to hold for a pointer, because they might not point anywhere on initialization or to one object with a different lifetime than the structure it's contained in.
2
u/SmokeMuch7356 Aug 08 '26 edited Aug 08 '26
Writing
a[i]is a lot cleaner than*(a + i)and you're less likely to make a mistake, especially ifiis a complex expression. And as you add array dimensions it gets a lot uglier in a hurry. Would you rather writeor
?
Note that
*a + iwill do something completely different.A little history...
C was derived from Ken Thompson's B programming language. When you created an array in B:
an extra word was set aside to store the address of the first element of the array:
The array subscript operation
a[i]was defined as*(a + i)- offsetiwords from the address stored inaand dereference the result.Ritchie wanted to keep B's array behavior -
a[i] == *(a + i)- but he didn't want to store the pointer that behavior required. Instead, he came up with a rule that an array expression will "decay" or evaluate to the address of the first element of the array. When you declare an array in C:you get:
The subscript operation
a[i]is still defined as*(a + i), but instead of storing a pointer value,aevaluates to a pointer value.You can use the subscript operator with regular pointers too:
2
u/burlingk Aug 09 '26
So, two things:
First, your note:
manages to say the opposite of the first part. It seems like you are saying "Why should I use subscripting at all. Note, Subscripting is good." SO, you might want to clarify that a bit rather than just adding a note.
Second: It is all about context. Pointers and subscripts do different things.
And even when you are using them to do the same thing: Sometimes people use the pointers just to be fancy. Other times they use them for performance reasons, or because they want to access the data in different ways.
2
u/LordRybec Aug 13 '26
Once you've learned C, go learn some assembly. I recommend a simpler one, like ARM or even 8051 (or MSP430, if you want ultra simple) over Intel, which is overly complicated if you aren't already experienced in assembly. That's probably the most effective way to understand pointers at a really deep level. Once you've done that, you'll find that sometimes pointers make more sense in C and other times subscripting is better. It's not so much about the compiler (modern compilers are really good at optimizing regardless of which you choose) as it is about understanding the code. Just C itself isn't great for understanding pointers well. (Trust me, I thought I knew pointers really well...) Assembly shows you how it works under the hood, and once you've got that down, you'll understand C better than you thought you could, and especially pointers. (Also unions and structs. Learning assembly is good for understanding those very deeply as well.)
2
u/NoSpite4410 Aug 13 '26
As soon as you start using multi-dimensional arrays, the bracket syntax makes much more sense.
notice that
Arr[5][4]is both an lvalue expression and an rvalue expression.You can put it on the left or the right side of an assignment.
Arr[5][4] = 12;int x = Arr[5][4];1
u/IdealBlueMan Aug 08 '26
A big reason to use subscripts for arrays is that someone will eventually read your code and try to understand it. That’s a good general reason to stick with conventions by default.
That person will be used to subscripts and will have to do an (albeit simple) translation in their head, and that means they’ll have less mental energy to focus on what your code is doing.
0
u/[deleted] Aug 08 '26
[deleted] — view deleted comment