C++ Arrays

  1. What is Arrays

    Array is a collection of the same type of data that is accessed by something named index. Index is number from 0 to array length. Array length is how many items are in the array.

  2. Creating array

    To create an array you specify type and name and then you use [] to mark it is array. Inside [] you specify how many elements array could hold.

    // array of 5 integers
    int numbers[5];   

    To set array element you use name [] and inside the index.

    // first element will be 0 because index is from 0 to array length
    numbers[0] = 0;

    To access value of array element you are using the same as setting element.

     int a;
     // a would be set to 0
     a = numbers[0];

    The numbers itself is a pointer to first element of an array. You can access first element by dereferencing it using *. If you access array element that is not inside array you could get some bad errors and memory leaks so don't do it.

    *numbers // 0