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

This is a discussion on Why use [3] when first is [0]? within the C++ Programming forums, part of the General Programming Boards category; If you need 3 things stored in an array why is it that so many people use an array of ...

  1. #1
    Shadow12345
    Guest

    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?

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    28
    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.

  3. #3
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Your right!

    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?


    null terminator (I think that's the correct name).
    Yes, your correct.
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  4. #4
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    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]
    Last edited by OneStiffRod; 11-08-2002 at 06:04 PM.
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    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.

  6. #6
    Shadow12345
    Guest
    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

  7. #7
    Shadow12345
    Guest
    int array[3] reserves memory for 3 arrays
    You mean an array of 3 instances of its type

  8. #8
    Shadow12345
    Guest
    ok well onestiffrod answered it, if he is correct he confirmed what i thought

    sorry for the triple post

  9. #9
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    He's correct.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  10. #10
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Originally posted by Shadow12345
    You mean an array of 3 instances of its type
    yes, i typed it too fast to notice what i typed.

  11. #11
    Registered User
    Join Date
    Sep 2002
    Posts
    72
    array[0] must be blank im pretty sure of this.

  12. #12
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    array[0] is NOT blank. It's the first valid element in the array.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  13. #13
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Originally posted by niroopan
    array[0] must be blank im pretty sure of this.
    You are?
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  14. #14
    Banned Cgawd's Avatar
    Join Date
    Oct 2002
    Posts
    405
    lol maybe everyone should google arrays and read

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stone Age Rumble
    By KONI in forum Contests Board
    Replies: 30
    Last Post: 04-02-2007, 09:53 PM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. sscanf and memory allocation
    By groorj in forum C Programming
    Replies: 3
    Last Post: 04-26-2005, 08:31 AM
  4. Replies: 1
    Last Post: 04-08-2003, 11:39 PM
  5. 2D table problem
    By GaPe in forum C Programming
    Replies: 1
    Last Post: 12-10-2001, 02:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21