negative index of array

hi guys can you explain what the meaning of negative index. i read someone code and don’t understand because there like these:

arr1[-1]='0';

so i search on net still i don’t find an answer.

thx for answer.

It depends on which language the program you are referring to is written.

In Python, it has a clear and defined meaning. In C/C++, it has a meaning, but it can give you errors, sometimes. In Java, it is an error.

So, please specify which language the program was written in. Or, you could share a link to the program itself.

2 Likes

You sure it had ‘;’ at the end of the statement, coz python doesn’t have such a syntax. Though arr[-1] in python refers to the last element of the list.

its on c++

In C/C++, the element a[i] just means go to the ith element starting from the base address of array a.

So, a[0] means the base address of a itself. a[1] means the address that immediately follows the base address. And so on…

For simplicity, assume that an int takes only 1 byte and that the base address of a is 1000. So, a[0] is the element in address 1000, a[1] is the element in the address 1001, …, a[57] is the element in the address 1057, and so on… Definitely, a[i] means element in the address 1000 + i.

So, a[-1] means the element in the address 1000 + -1, which is element in the address 999.

In other words, the integer, that precedes even the first element of the array.

Now, there are two cases that can arise:

  1. The above said location is restricted for access. Then, we get a Runtime Error (Segmentation Fault).
  2. No restriction for access. The element at that address can be read from or written into. However, this kind of behaviour is clearly undefined, as to we don’t know what exactly is getting stored in that location.

If we specifically know what is there in that location, it is ok. But otherwise, that code is, allow me to use some compiler warning terminology, dangerous and should not be used.

I am wondering, what exactly that code was trying to do. Can you share the link to the program you were talking about? Someone here could be of better help, making you understand what is going on in that particular program.

Cheers!

If it’s on c++ then, your program works fine in most of the cases and in few it crashes. The, reason behind this,

Suppose you declare an array of size 10. i.e., a[10]. Now, what actually happens is that a 10 block of memory is allocated for this. And it can be from 100001 to 1000010 or 2938983 to 2938993. So, your 0 will be stored in 100000 for the first case and 2938982 for the second case which are perfectly valid. But, sometimes these can be non-valid or have access restrictions then, your program crashes. So, avoid using this.

It works in python.Just follow the below program your concept will be clear
family = [‘mom’,‘dad’,‘bro’]

print “List of family using positive index :”

print family[0]

print family[1]

print family[2]

print “”

print “List of family using negative index :”

print family[-3]

print family[-2]

print family[-1]

output: