Yes. 6.5.2.1 paragraph 1 (C99 standard) describes the arguments to the [] operator:

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

6.5.2.1 paragraph 2 (emphasis added):

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).

It says nothing requiring the order of the arguments to [] to be sane.

Answer from Chris Lutz on Stack Overflow
Top answer
1 of 2
42

Yes. 6.5.2.1 paragraph 1 (C99 standard) describes the arguments to the [] operator:

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

6.5.2.1 paragraph 2 (emphasis added):

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).

It says nothing requiring the order of the arguments to [] to be sane.

2 of 2
37

In general 2[a] is identical to a[2] and this is guaranteed to be equivalent in both C and C++ (assuming no operator overloading), because as you meantioned it translates into *(2+a) or *(a+2), respectively. Because the plus operator is commutative, the two forms are equivalent.

Although the forms are equivalent, please for the sake of all that's holy (and future maintenance programmers), prefer the "a[2]" form over the other.

P.S., If you do get asked this at an interview, please do exact revenge on behalf of the C/C++ community and make sure that you ask the interviewer to list all trigraph sequences as a precondition to you giving your answer. Perhaps this will disenchant him/her from asking such (worthless, with regard to actually programming anything) questions in the future. In the odd event that the interviewer actually knows all nine of the trigraph sequences, you can always make another attempt to stomp them with a question about the destruction order of virtual base classes - a question that is just as mind bogglingly irrelevant for everyday programming.

🌐
Reddit
reddit.com › r/c_programming › why is array[index] == index[array] ?
r/C_Programming on Reddit: Why is array[index] == index[array] ?
July 28, 2022 - ... Addition vs. concatenation ... Because addition is commutative. Array[Index] is equivalent to *(Array + Index) which is equivalent to *(Index + Array) which is equivalent to Index[Array].
Discussions

language design - why are both index[array] and array[index] valid in C? - Software Engineering Stack Exchange
Stack Exchange network consists ... share their knowledge, and build their careers. Visit Stack Exchange ... Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... then both index[array] and array[index] are valid expressions, much ... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
May 2, 2012
What is the correct type for array indexes in C? - Stack Overflow
What type for array index in C99 should be used? It have to work on LP32, ILP32, ILP64, LP64, LLP64 and more. It doesn't have to be a C89 type. I have found 5 candidates: size_t ptrdiff_t intptr_t / More on stackoverflow.com
🌐 stackoverflow.com
Indexing of array in c programming
In C programming, an array is a collection of elements of the same type stored in contiguous memory locations. Indexing is the way to access individual elements of the array. More on askfilo.com
🌐 askfilo.com
1
November 26, 2025
arrays - index in c programming - Stack Overflow
I have a question about locating an index. suppose I have a "relative" index in an array (that was allocated with malloc), or basically an index that doesn't tell me where I am really. how can I f... More on stackoverflow.com
🌐 stackoverflow.com
May 22, 2017
🌐
GNU
gnu.org › software › c-intro-and-ref › manual › html_node › Accessing-Array-Elements.html
Accessing Array Elements (GNU C Language Manual)
If the variable a is an array, ... Since the variable a is an lvalue, a[n] is also an lvalue. The lowest valid index in an array is 0, not 1, and the highest valid index is one less than the number of elements....
🌐
Wikibooks
en.wikibooks.org › wiki › C_Programming › Arrays_and_strings
C programming/Arrays and strings - Wikibooks, open books for an open world
August 11, 2003 - Arrays in C are indexed starting at 0, as opposed to starting at 1. The first element of the array above is point[0]. The index to the last value in the array is the array size minus one. In the example above the subscripts run from 0 through 5. C does not guarantee bounds checking on array ...
🌐
GeeksforGeeks
geeksforgeeks.org › c language › c-arrays
Arrays in C - GeeksforGeeks
Array indexing starts from 0, so the first element is stored at index 0. Arrays can store multiple values of the same data type under a single variable name. C supports one-dimensional arrays as well as multidimensional arrays such as 2D and ...
Published: 3 weeks ago
🌐
CS UIC
cs.uic.edu › ~jbell › CourseNotes › C_Programming › Arrays.html
C Programming Course Notes - Arrays
VERY IMPORTANT: Array indices start at zero in C, and go to one less than the size of the array. For example, a five element array will have indices zero through four. This is because the index in C is actually an offset from the beginning of the array. ( The first element is at the beginning ...
🌐
LabEx
labex.io › questions › how-to-access-elements-of-an-array-in-c-136083
How to Access Array Elements in C | LabEx
July 25, 2024 - In C, array indices start at 0, so the first element of an array is at index 0, the second element is at index 1, and so on.
Find elsewhere
🌐
Medium
medium.com › @vijethumesh › does-0-myarray-indexing-work-in-c-8953603529fb
Does 0[myArray] Indexing Work in C? | by Vijeth | Medium
February 5, 2024 - In C, an array is a collection of elements of the same data type that are stored in contiguous memory locations. These elements can be accessed using an index(it starts from 0).
Top answer
1 of 6
12

First of all, it would help to read dmr's Development of the C Language to get some insights into some of C's quirks, particularly when it comes to array semantics (basically, blame BCPL and B for most of it).

As for the question "[w]hy not just enforce that index[array] is invalid, for clarity's sake," what would such a check buy you in exchange for the cost of performing it? The form almost never appears outside of the IOCCC, so it's not like it's a major problem in production code (compared to the use of, say, gets, or unchecked array accesses (which disallowing i[a] won't help with), or <fill in the blank>). It's not a bug; it doesn't introduce any undefined behavior; it doesn't introduce any security holes not already present with a[i]; the only complaints against it are stylistic in nature.

It's like asking why both T *p and T* p are valid; there is no "why" beyond it being an accident of the language syntax. There's nothing deliberate behind allowing both, it's just a function of how the grammar works. Same with a[i] and i[a]. Professional programmers are (usually) grown-ups, and don't deliberately introduce confusion where it isn't warranted, so most will naturally use a[i].

You're basically trying to guard against a problem that doesn't really exist.

2 of 6
5

The choice to use array[index] was probably made to follow mathematical convention and the precedent set for arrays by other languages like ALGOL, FORTRAN, and BASIC (the latter two use parentheses instead of brackets). That decision does make the operator an odd duck because it's binary operator but requires that you throw in an additional token after the right-hand expression.

The operator could just as easily have been a single character (@ isn't spoken for in C, so let's call that the "pointer addition operator"). Because, as David Thornley's answer in the aforementioned SO question points out, pointer addition is commutative, a @ 5 and 5 @ a make equal sense. (C forerunner BCPL used ! for this.)

In some ways it's natural to look at the array operator as behaving like a function, which makes sense in the context of languages that do f(x) to call a function and a(i) to access an array. Since operators in C don't work that way, you have to think of it as a commutative, binary operator with baggage.

Top answer
1 of 9
46

I think you should use ptrdiff_t for the following reasons

  • Indices can be negative. Therefore for a general statement, all unsigned types, including size_t, are unsuitable.
  • The type of p2 - p1 is ptrdiff_t. If i == p2 - p1, then you should be able to get p2 back by p2 == p1 + i. Notice that *(p + i) is equivalent to p[i].
  • As another indication for this "general index type", the type of the index that's used by overload resolution when the builtin operator[] (for example, on a pointer) competes against a user-provided operator[] (for example vector's) is exactly that (http://eel.is/c++draft/over.built#16): >

    For every cv-qualified or cv-unqualified object type T there exist candidate operator functions of the form

    T*      operator+(T*, std::ptrdiff_t);
    T&      operator;
    T*      operator-(T*, std::ptrdiff_t);
    T*      operator+(std::ptrdiff_t, T*);
    T&      operator;
    

EDIT: If you have a really big array or a pointer to a really big memory portion, then my "general index type" doesn't cut it, as it then isn't guaranteed that you can subtract the first element's address from the last element's address. @Ciro's answer should be used then https://stackoverflow.com/a/31090426/34509 . Personally I try to avoid using unsigned types for their non-ability to represent negative edge cases (loop end-values when iterating backwards for example), but this is a kind of religious debate (I'm not alone in that camp, though). In cases where using an unsigned type is required, I must put my religion aside, of course.

2 of 9
34

I almost always use size_t for array indices/loop counters. Sure there are some special instances where you may want signed offsets, but in general using a signed type has a lot of problems:

The biggest risk is that if you're passed a huge size/offset by a caller treating things as unsigned (or if you read it from a wrongly-trusted file), you may interpret it as a negative number and fail to catch that it's out of bounds. For instance if (offset<size) array[offset]=foo; else error(); will write somewhere it shouldn't.

Another problem is the possibility of undefined behavior with signed integer overflow. Whether you use unsigned or signed arithmetic, there are overflow issues to be aware of and check for, but personally I find the unsigned behavior a lot easier to deal with.

Yet another reason to use unsigned arithmetic (in general) - sometimes I'm using indices as offsets into a bit array and I want to use %8 and /8 or %32 and /32. With signed types, these will be actual division operations. With unsigned, the expected bitwise-and/bitshift operations can be generated.

🌐
Cprogramming
cprogramming.com › tutorial › c › lesson8.html
Arrays in C - Cprogramming.com
How to begin Get the book · C tutorial C++ tutorial Game programming Graphics programming Algorithms More tutorials
🌐
GNU
gnu.org › software › c-intro-and-ref › manual › html_node › Pointers-and-Arrays.html
Pointers and Arrays (GNU C Language Manual)
The clean way to refer to an array element is array[index]. Another, complicated way to do the same job is to get the address of that element as a pointer, then dereference it: * (&array[0] + index) (or equivalently * (array + index)). This first gets a pointer to element zero, then increments ...
🌐
Substack
alexanderobregon.substack.com › alexander obregon's substack › arrays with pointer indexing in c
Arrays with Pointer Indexing in C
January 22, 2026 - On a machine where sizeof(int) is 4, the array occupies 16 bytes as one contiguous region. The first element of temps has the lowest address within that region, the second element follows right after it, and so on until the last element. Indexing temps[i] tells the compiler to start at the address of the first element, add i times the size of int, and access the result as an int.
🌐
Filo
askfilo.com › higher education › smart solutions › indexing of array in c programming
Indexing of array in c programming... | Filo
November 26, 2025 - #include <stdio.h> int main() { ... 100; printf("Modified fifth element: %d\n", arr[4]); // Output: 100 return 0; } Arrays in C are zero-indexed....
🌐
Sc
people.math.sc.edu › Burkardt › c_src › index › index.html
INDEX - Indexing Multidimensional Arrays
INDEX is a C library which can convert the index of a multidimensional array entry into the corresponding index of a one dimensional vector, using either row or column major indexing, with zero or unit base.
🌐
Quora
quora.com › Can-the-index-of-an-array-in-C-start-from-1
Can the index of an array in C start from 1? - Quora
Answer (1 of 5): You can happily ignore the 0th index in an array and start using from the second position (or later) if you want to. Sometimes this is useful if it’s more logical to map the index to an actual number or rank, e.g. x[5] contains the 5th value of ‘x’. Disadvantages are ...
🌐
Sanfoundry
sanfoundry.com › c-tutorials-pointer-more-efficient-than-subscript-accessing-array
Pointers vs Array Indexing in C - Sanfoundry
December 31, 2025 - Conclusion: Pointers may offer better performance in highly optimized or specialized scenarios, but modern compilers usually eliminate the speed gap. Arrays: The syntax arr[i] clearly conveys “the i-th element” and is intuitive to most developers, making the code easier to read, debug, and maintain.
🌐
TutorialKart
tutorialkart.com › c-programming › c-find-index-of-specific-element-in-array
Find Index of Specific Element in Array in C
October 19, 2021 - #include <stdio.h> int main() { int arr[] = {2, 4, 6, 8, 10}; int x = 8; int arrLen = sizeof arr / sizeof arr[0]; int index = -1; for (int i = 0; i < arrLen; i++) { if (arr[i] == x) { index = i; break; } } if (index > -1) { printf("Index : %d\n", index); } else { printf("%d is not present in this array.\n", x); } return 0; } ... In this C Tutorial, we learned how to find the index of a specific element in given Array, with examples.
🌐
W3Schools
w3schools.com › c › c_arrays.php
C Arrays
We have now created a variable that holds an array of four integers. To access an array element, refer to its index number.