simple array question

This is a discussion on simple array question within the C++ Programming forums, part of the General Programming Boards category; I don't know how to put this in a form of a question but for example, doesn't a sample array ...

  1. #1
    Unregistered
    Guest

    simple array question

    I don't know how to put this in a form of a question but for example, doesn't a sample array initiation "int array[4];" hold 5 elements because the elements start from array[0]?

    so the array broken down would be:
    array[0] , array[1] , array[2] , array[3] , array[4], right?

    if so, then why do I get a page fault error from this code?
    any help is appreciated.

    [code]

    #include<iostream.h>

    int main(void)
    {
    int array[4];

    for(int x=0; x<=4; x++)
    {
    array[x] = x;
    }

    return 0;
    }

    [\code]

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    20
    no, the number you put in the declaration is the number of elements, not the index of the highest element.
    In other words, int ar[4]; gives 4 elements from ar[0]to ar[3].
    Ciao Al

  3. #3
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    Heh. It was answered.
    int array [5];

    means you can have
    array[0, 1, 2, 3, 4] and nothing more.

    So, if the size of the array is n, the max storage place is n-1 and the array's first storage place is 0.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hopefully simple question, input streams
    By dpro in forum C++ Programming
    Replies: 7
    Last Post: 03-09-2006, 01:59 PM
  2. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM
  3. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM
  4. simple array question
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 12-03-2001, 04:46 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 10:48 AM

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