454,486 Members | 1,696 Online
Need help? Post your question and get tips & solutions from a community of 454,486 IT Pros & Developers. It's quick & easy.

# Array numbering starting from 0 instead of 1

 P: n/a Hi! I teach C++ in schools in India. I don't have a good answer when students ask me why arrays in C++ are numbered from 0 to n-1 for an array of n elements. I hope somebody can tell me. Thanks Binoy Nov 8 '08 #1
9 Replies

 P: n/a bintom wrote: Hi! I teach C++ in schools in India. I don't have a good answer when students ask me why arrays in C++ are numbered from 0 to n-1 for an array of n elements. I hope somebody can tell me. I guess one answer is because C does. Having started many years ago as an assembler programmer, I've always thought of arrays as pointers to memory. The offset of the first element is zero, which is the array index zero. Thus given char n[4]; n+0 is equivalent to n[0]; -- Ian Collins Nov 8 '08 #2

 P: n/a On Nov 8, 1:40 am, bintom 20. If you would exclude 0 why not exclude 10 and 20 as well? Those languages that do use index 1->10 in an array[10] allocate 11 elements and the first one is ignored / waisted. Nov 8 '08 #3

 P: n/a bintom wrote: Hi! I teach C++ in schools in India. I don't have a good answer when students ask me why arrays in C++ are numbered from 0 to n-1 for an array of n elements. I hope somebody can tell me. The reason why it's like that in C++: The array notion a[b] is equivalent to *(a+b), where usually, a is the address of the array's first element, and b is the index. So if you want the first element, b must be 0. Basically, that's how all computers do it. So if you have a language that starts at 1, like e.g. Matlab does, the interpreter has to subtract 1 from every index or alternatively leave the first element blank. Generally, I'd rather ask why we tend to start counting at 1 instead of 0, but my guess is that this has historical reasons (for quite a long time, there was no number 0). Sometimes, however, we do count from 0, and sometimes, we even mx it up, like e.g. time. A day starts at hour 0, but a month starts at day 1. Kind of strange, isn't it? Nov 8 '08 #4

 P: n/a On 8 Nov, 06:52, Ian Collins

 P: n/a On Fri, 7 Nov 2008 22:40:51 -0800 (PST), bintom

 P: n/a On Nov 8, 9:47*am, Rolf Magnus

 P: n/a bintom

 P: n/a It might make the pill easier to swallow if you mention that zero is one of India's great contribution to mathematics, along with a slew of famous mathematicians. "bintom"

 P: n/a On 8 Nov., 07:52, Ian Collins char n[4]; n+0 is equivalent to n[0]; -- Ian Collins Nov 10 '08 #10

### This discussion thread is closed

Replies have been disabled for this discussion.