Arrays are built-in containers and do not require any library or template for initialization. Among other things, the main difference between vectors and arrays is that arrays cannot be extended. Once their size is defined, you cannot add elements to the array, you can only alter the data stored in the array.

Initializing an Array

To initialize an array, we need to mention type of elements and dimension along with the array name. Here

  • arr is the name of the array
  • [4] is the dimension
  • int is the type of elements stored in arr

Note: Arrays can only store objects. Hence you can have array of pointers but not of references (clear bias against references ).

Accessing Values

To access value at a particular location, you can subscript the position of the element you want to access.

Arrays have a zero based index. Essentially, the first element has the index 0, the second element has index one and so on.

Array Pointers

Like any other object, Arrays too can have pointers and references. Defining a pointer to an array is fairly simple.

Value Initialization

Variables that are value initialized get the default value for the type they represent. For instance, a string object is value initialized to ''(empty string) while an int is value initialized to 0.

Here, we have initialized an array arr which contains 4 int elements. In the next line, we declare a pointer to int p1 and assign arr to it. arr here is converted to a pointer automatically.

Note: Whenever we assign an array to a variable, the array is automatically converted to a pointer. This pointer points to the first element in the array.

p2 is another pointer which also points to the first element in arr. But you can clearly see that simply assigning an array to a variable is easier for creating a pointer. Pointer arrays come in handy when iterating over an array.

Also, you may declare a pointer with the auto specifier. auto will automatically detect the type of the variable from the initializer.

In this case, since arr is an array of int, p will automatically be converted to a pointer to the first element in arr.

Also, complex way for creating a pointer to an array.

This way of declaring pointers can be confusing as we need to place the parentheses properly. For instance:

This distinction is very important and can lead to various undefined errors.

Standard Library Functions

The standard library also provides two specific functions for converting arrays into pointers. These functions include:

  • begin: returns pointer to the first element
  • end: returns pointer to one past the last element

These are handy methods for quickly creating pointers and are helpful in iterating over the array.

Multi-Dimensional Arrays

By default, C++ does not support multi-dimensional arrays. Instead, we have array of arrays. It’s a little like the movie inception.

In a two dimensional array, the first dimension defines the number of columns and the second dimension defines the number of rows.

Accessing Values

You need to subscript twice to access value at a particular location.

Iterating Over Arrays

Like with any other container, we may need to iterate on arrays. There are various ways of doing so.

Range For

The most simple one, of course, is the range for loop.

Range For

These loops were introduced in the C++ 11 standard and provide an easy way of iterating over any container. When used with auto, they can provide a lot of flexibility to the developer.

In this loop, i is initialized with the first element of arr. After the first iteration, c is automatically initialized with the next element in arr on each subsequent iteration.

Pointers

The fact that variables initialized with arrays are converted to pointers can be leveraged for easily iterating over an array. Apart from the array, we’ll also require the size of the array.

size_t: An unsigned type for specifying size of array and for subscripting a particular element. Unsigned types are greater than or equal to 0. Used for storing larger positive integers and for avoiding negative numbers.

constexpr: Similar to const but ensures that initializer is a constant expression.

Here, we specify the size of array as a constexpr(constant expression.) The type of arr_size is size_t. We initialize our array arr. Now to iterate over the values, we use a for loop (a while loop can be used as well.) For the control variable, we use an int pointer p. As we already know, initializing p with arr will store the memory address of the first element in the pointer. After this, on each iteration, we want to check that the size p refers to the off-the-end pointer for arr. If it does, we want to stop the iteration. Also, on each iteration, one is added to p.

Pointer Arithmetic

Pointers work like iterators. When you add 1 to a pointer, it points to the next element in the array. Type of pointer plays a crucial role when working with pointer arithmetic.

Off-the-end pointer

Pointer that points to one past the last element in an array.

Standard Library Functions

The standard library begin and end functions can be used for iteration.

begin points to the first element in arr and end one past the last element. When begin is equal to end, the iteration will stop.

Iterating over Multi-Dimensional Arrays

Same principles can be used for iterating over multi-dimensional arrays. Except this time, we’ll need loops equal to the number of subscripts in the array.

Here, the outer loop iterates over each row and the inner loop iterates over elements of the inner arrays. Remember though, too many dimensions can fuck things up for you.

Conclusion

Arrays are powerful containers just like vectors. They offer better performance at the cost of flexibility. Each container has its benefits. If you know the number of elements you’ll be storing in a container, then arrays would be the right choice.

Peace Out!


Weird Programmer

I hate people who use LOL in face to face conversations. Also, if I tell you anything else about myself, then I'll have to KILL you~~!

Leave a Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of
%d bloggers like this: