Arrays start from 0 then 1,2... But why are indexes positive, why are indexes not negative?
Asked
Active
Viewed 181 times
-1
-
In some languages you can index of lots of thing, including a range of negatives or an enumeration type. – Buffy Oct 27 '18 at 00:44
-
Okk but I try to gave negavtive index in c++ then its show error.is this an other way to gave negative index ?? – Iram Shah Oct 27 '18 at 03:35
-
What are you trying to achieve with the negative index? – OBu Oct 27 '18 at 18:43
-
I am a student and all teachers tell us only about positive index in array. So there is a question in my mind that is it possible to gave negative index if yes the. How is it possible – Iram Shah Oct 27 '18 at 20:21
-
In COBOL, array indexes start at 1. Then the compiler, under the covers, converts the index to a zero based offset from a base pointer. – pojo-guy Oct 28 '18 at 02:17
-
So its mean if I want to gave negative index number that start from -1 then firstly I should set compiler as I want ?? – Iram Shah Oct 28 '18 at 03:46
-
it means it's up to the language to decide what the index number really means. E.g. in python, lists can have an index like my_list[-1] - which is the last element in the list. – OBu Oct 28 '18 at 12:42
-
2Hi @IramShah, welcome to [cseducators.se]! I'm afraid that I had to close this question, as it is off-topic for the site. CSEd is mostly for teachers, and it is about how to teach CS concepts. So a question about classroom practice would be topical here. However, I see that you already received a good answer, so I hope that you also got the help that you need! – Ben I. Oct 28 '18 at 18:22
-
Ok............. – Iram Shah Oct 28 '18 at 19:20
-
“Everything has to start somewhere”: if it starts at zero, then it can not start at a negative number, in the same way that it can not start at 42 (because it starts at zero). – ctrl-alt-delor Oct 31 '18 at 17:40
1 Answers
4
You are starting with a base memory address and you are adding an offset having the size of one array element for each following element. This makes address calculation very easy (base address + n * size of data structure).
Negative indexes are not allowed because you would refer to the memory address in front of your base address, which means you might destroy other variables content.

ctrl-alt-delor
- 10,635
- 4
- 24
- 54

OBu
- 594
- 3
- 15
-
1Re, "...this makes address calculation very easy..." That is a reason why arrays can be more efficiently implemented if the range of allowed indices is contiguous, but it does not explain why that range should not include any negative numbers. – Solomon Slow Oct 29 '18 at 19:51
-
2Re, "...you might destroy other variable's content." That is a reason why you should not use out-of-bounds array indices, but it does not explain why negative numbers should be out-of-bounds. – Solomon Slow Oct 29 '18 at 19:51
-
1There is no _strong_ reason why negative numbers typically are not allowed. Probably the best reason is, Many languages, for simplicity's sake, only allow the programmer to specify the size of an array, rather than allowing the programmer to specify a starting and ending index. If you want to choose one starting index that will make everybody happy, there are not very many reasonable choices. Some programmers will grumble if you choose 0. Some will grumble if you choose 1. _Everybody_ will grumble (and, nobody will use your language) if you choose any other number. – Solomon Slow Oct 29 '18 at 20:01
-
2Consider "The principle of least surprise". Or, minimum grumbling. Maybe negative indices would refer to imaginary data? Probably appropriate for quantum computing. – Scott Rowe Oct 31 '18 at 12:14