# Why use [3] when first is [0]?

• 11-08-2002
Why use [3] when first is [0]?
If you need 3 things stored in an array why is it that so many people use an array of [3] when the first is [0]? Wouldn't you only need [2]?
for example
float m_location[3];
That is used to store the x, y, and z coordinates of a vertex. However, I do not see why
float m_location[2];
wasn't used instead. Can someone shed some light on this topic?
• 11-08-2002
Will
if it's a character array, then the last space is taken up by a null terminator (I think that's the correct name). it's represented as \0.

so if I wanted to store "dog" in char array[3], the memory would look like this:

Code:

```Array Position:  0    1    2    3 Character:        D    O    G    \0```
other than that, I think people just forget that arrays start at 0. I'm probably wrong though. :o
• 11-08-2002
correlcj
See similar code the face master produced earlier and see.
I see because it starts at 1 instead of 0 as i to think.

Code:

```#include <iostream> using namespace std; int main() { int array[10]; for(int i = 10; i > 0; i--)  //this is set to 1 instead of 0 {     array[i] = i; } // just to check its all been done properly... for(int j = 10; j > 0; j--) {     cout << array[j];     cout << "\n"; } return 0; }```
Is this what you mean?
:D

Quote:

null terminator (I think that's the correct name).
• 11-08-2002
OneStiffRod
When you declare an array the number is the total indices and not the maximum count.

int array[3];

equals ...

array[0] , array[1], array[2];

If you said...

int array[2]; // Then it's only

array[0], array[1];

//The maximum [val] is (n-1) so int array[7]; would be array[0] through array[6]
• 11-08-2002
alpha
okay, when you first declare the array, int array[3] reserves memory for 3 arrays, index numbers of 0, 1, 2. when you refer to them after declaration, then they are refered to by index number, but the declaration tells the computer how many slots you want in your array.

edit: beaten.
• 11-08-2002
Yes I knew that aboiut character arrays, but I am talking about arrays of virtually every other data type. I always see people use :
float mp_verticies[3]
cat listofcats[3]
CVector3 LineNormals[3]
when three instances of each are all that is needed, yet enough space is set aside for 4
• 11-08-2002
Quote:

int array[3] reserves memory for 3 arrays
You mean an array of 3 instances of its type
• 11-08-2002
ok well onestiffrod answered it, if he is correct he confirmed what i thought

sorry for the triple post
• 11-08-2002
skipper
He's correct. :)

-Skipper
• 11-08-2002
alpha
Quote:

You mean an array of 3 instances of its type
yes, i typed it too fast to notice what i typed.
• 11-09-2002
niroopan
array[0] must be blank im pretty sure of this.
• 11-09-2002
SilentStrike
array[0] is NOT blank. It's the first valid element in the array.
• 11-09-2002
Sang-drax
Quote:

Originally posted by niroopan
array[0] must be blank im pretty sure of this.
You are?
• 11-09-2002
Cgawd