Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are negative array indexes allowed in C?

Tags:

arrays

c

People also ask

Can array take negative values?

No, you cannot use a negative integer as size, the size of an array represents the number of elements in it, –ve number of elements in an array makes no sense.

Can you use negative indexes in C++?

C++ compiler will assign the value at negative index as 0. You can check that in the ide. You should prevent negative indexes in the array as it may have some unexpected results depending on the compiler. Here it is not causing any error and taking cumSum[-1] as 0.

Can the subscript in array be negative?

You can specify a negative subscript only when you use an upper bound of -1 to declare an empty array. Such an array has zero elements, but it is not Nothing .

Does C# have negative indexing?

Negative values are not permitted as index values by the C# compiler, which is more strict than C/C++ compilers which leave it to the programmer to not supply negative index values.


That is correct. From C99 §6.5.2.1/2:

The definition of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2))).

There's no magic. It's a 1-1 equivalence. As always when dereferencing a pointer (*), you need to be sure it's pointing to a valid address.


This is only valid if arr is a pointer that points to the second element in an array or a later element. Otherwise, it is not valid, because you would be accessing memory outside the bounds of the array. So, for example, this would be wrong:

int arr[10];

int x = arr[-2]; // invalid; out of range

But this would be okay:

int arr[10];
int* p = &arr[2];

int x = p[-2]; // valid:  accesses arr[0]

It is, however, unusual to use a negative subscript.


Sounds fine to me. It would be a rare case that you would legitimately need it however.


What probably was that arr was pointing to the middle of the array, hence making arr[-2] pointing to something in the original array without going out of bounds.


I'm not sure how reliable this is, but I just read the following caveat about negative array indices on 64-bit systems (LP64 presumably): http://www.devx.com/tips/Tip/41349

The author seems to be saying that 32 bit int array indices with 64 bit addressing can result in bad address calculations unless the array index is explicitly promoted to 64 bits (e.g. via a ptrdiff_t cast). I have actually seen a bug of his nature with the PowerPC version of gcc 4.1.0, but I don't know if it's a compiler bug (i.e. should work according to C99 standard) or correct behaviour (i.e. index needs a cast to 64 bits for correct behaviour) ?


I know the question is answered, but I couldn't resist sharing this explanation.

I remember Principles of Compiler design: Let's assume a is an int array and size of int is 2, and the base address for a is 1000.

How will a[5] work ->

Base Address of your Array a + (index of array *size of(data type for array a))
Base Address of your Array a + (5*size of(data type for array a))
i.e. 1000 + (5*2) = 1010

This explanation is also the reason why negative indexes in arrays work in C; i.e., if I access a[-5] it will give me:

Base Address of your Array a + (index of array *size of(data type for array a))
Base Address of your Array a + (-5 * size of(data type for array a))
i.e. 1000 + (-5*2) = 990

It will return the object at location 990. So, by this logic, we can access negative indexes in arrays in C.


About why would someone want to use negative indexes, I have used them in two contexts:

  1. Having a table of combinatorial numbers that tells you comb[1][-1] = 0; you can always check indexes before accessing the table, but this way the code looks cleaner and executes faster.

  2. Putting a centinel at the beginning of a table. For instance, you want to use something like

     while (x < a[i]) i--;
    

but then you should also check that i is positive.
Solution: make it so that a[-1] is -DBLE_MAX, so that x&lt;a[-1] will always be false.