6

I initially thought that it has a different use for pointers and for arrays. In the former case, it adds whatever is in brackets to the pointer and then dereferences the sum; in the latter case it would just yield the ith element of an array.

Then I realized that an array variable returns the pointer to the first element, so the operator does the same thing in each case: offset and dereference.

Does the bracket [] operator indeed only have a single use in C?

  • 3
    That's like saying "Is there only *one* use for the `+` operator? All it does is *add* things". – user229044 Nov 05 '13 at 04:55
  • I'm asking for two reasons: 1) I'm not sure if my observation is accurate (the claim about it having the same functionality for pointers and arrays) and 2) perhaps it is used for other things I don't know about (like *). – Wuschelbeutel Kartoffelhuhn Nov 05 '13 at 04:57

2 Answers2

15

[] is called array subscript operator, but syntactically it's used on a pointer. An array is converted to a pointer to the first element in this usage (and many others). So, yes, [] is the same for arrays and pointers.

C11 §6.5.2.1 Array subscripting

Constraints

One of the expressions shall have type ‘‘pointer to complete object type’’, the other expression shall have integer type, and the result has type ‘‘type’’.

Semantics

A postfix expression followed by an expression in square brackets [] is a subscripted designation of an element of an array object. The definition of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2))). Because of the conversion rules that apply to the binary + operator, if E1 is an array object (equivalently, a pointer to the initial element of an array object) and E2 is an integer, E1[E2] designates the E2-th element of E1 (counting from zero).

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • 4
    +1 this is correct. To address OP's core question, `[]` always operates on a pointer and an integer (in either order, btw). Arrays are never involved because, at the point where the `[]` operator is evaluated, any instance of an array name has already "decayed" to a pointer. – R.. GitHub STOP HELPING ICE Nov 05 '13 at 05:03
  • 4
    Fun fact, `array[offset] == offset[array]` is true. – diapir Nov 05 '13 at 05:04
  • however is it true and correct to use *(ptr+j) = somevalue when ptr is a pointer to element zero of an array and j is some offset index value? Seems to work well enough but is it correct? – paul lanken Nov 05 '13 at 05:08
  • 1
    @paullanken As long as the element is inside the range of the array, it's correct. – Yu Hao Nov 05 '13 at 05:10
  • @YuHao: thank you very much. What bothers me is that an array defined as uint64_t foo[32] consists of 8 byte sized unsigned integers and yet *(ptr+3) is the address of the 24th byte into the array if ptr = &foo[0]. Seems like compiler magic to me. – paul lanken Nov 05 '13 at 05:20
  • @paullanken That's because in pointer arithmetic, `ptr + 1` represents one **element** after `ptr`, not one byte. – Yu Hao Nov 05 '13 at 05:25
  • 1
    @WuschelbeutelKartoffelhuhn Exactly, if `ptr` is of type `int *`, then `ptr + 1` points 4 bytes after `ptr`, assuming `int` is 4 bytes. – Yu Hao Nov 05 '13 at 06:04
  • does this mean that if x is an array you can also do 3[x] to get the 3rd element of x in C? – Aristides Dec 07 '16 at 18:55
7

Whether it does "one thing" depends on what you think "one thing" means.

In C, the operator is defined like so

e1[e2]   means   *(e1+e2)

That's it. One thing. Or is it? Suppose a is an array and i is an integer. We can write:

a[3]
a[i]
3[a]
i[a]

and suppose p is a pointer and i is an integer. We can write

p[3]
p[i]
3[p]
i[p]

Arrays or pointers. Two things? Not really. You know that when we use the plus operator where one of the two operands is "an array" you are really doing pointer arithmetic.

The second part of your question - can it be used for things other than pointer arithmetic - is basically no in C, but yes in C++, because in C++ we can overload this operator. However sometimes you will see [] in type expressions, but that is probably not what you are asking about because in that case, we aren't really using it as an operator (we're using it as a type operator, which is different).

Ray Toal
  • 86,166
  • 18
  • 182
  • 232