As you already know, variables are necessary to store and process data of a certain type. But when you need to process multiple objects of the same type, you can save them in an array. Then process them together as a single unit (or take several variables and perform an action on each).
Overall, C++ arrays offer an efficient and straightforward method for storing and handling a set quantity of elements, all of which must share the same data type. They are widely used for tasks that require organizing data sequentially and allow for easy access and manipulation of the elements.
Arrays in C++
The picture below illustrates an array of five floating-point numbers. Each element has an integer index (0-4) to be accessed.
In C++, an array has the following important features:
Contiguous memory: An array stores its elements in contiguous memory locations. This means that the elements are stored one after another in memory, allowing for efficient access and traversal of the array.
Fixed size: Arrays have a fixed size determined at the time of declaration. Once the size is set, it cannot be changed during runtime.
Zero-based indexing: In C++, arrays use zero-based indexing. This means accessing the first element requires index 0, the second element with index 1, and so on. The last element is accessed by the index equal to array size – 1 (the array size referred to here is the total number of elements in the array);
Homogeneous elements: Arrays can only store elements of the same data type. All elements in an array must have the same size and type, ensuring consistency and efficient memory allocation.
Random access: You can access elements in an array randomly using their indices. This allows for direct access to any element in the array, making it easy to read or modify specific elements based on their position.
Size determination: You can determine the size of an array using the
sizeof()operator. This is particularly useful when dealing with arrays of built-in types to calculate the total memory occupied by the array.Array type: it is possible to create an array to store elements of any type (arrays can store elements of any valid C++ data type, including built-in types, user-defined types, and pointers).
Declaring and initialization
In C++, you can declare and initialize an array using the following syntax:
dataType arrayName[arraySize] = {element1, element2, ..., elementN};Here's a breakdown of the components:
dataType: This is the data type of the elements that will be stored in the array. It can be any valid C++ data type, such asint,float,char, etc.arrayName: This is the name you give to the array variable. Choose a descriptive name that reflects the purpose or content of the array. The rules for naming an array are the same as for a variable.arraySize: This is the number of elements the array can hold. It must be a non-negative integer value.{element1, element2, ..., elementN}: This is an optional list of initial values for the array elements. The number of elements in the initializer list must match the size of the array.
Here are a few examples:
int numbers[5] = {1, 2, 3, 4, 5}; // An array of integers with 5 elements
float grades[3] = {98.5, 85.0, 76.25}; // An array of floats with 3 elements
char vowels[5] = {'a', 'e', 'i', 'o', 'u'}; // An array of characters with 5 elementsIf you do not just declare an array, but immediately initialize it, then you can omit the array dimension:
int numbers[] = {1, 2, 3, 4, 5}; // Initialize an array of integersYou can also assign values to individual elements of the array using their indices after declaration.
int numbers[5];
numbers[0] = 654;
numbers[3] = 9;It's important to note that if you omit the initializer list, the elements of the array will be uninitialized, meaning they will contain garbage values. Just like with variables or pointers. That is why C++ is considered not the most secure programming language.
int numbers[5];
char vowels[5];
std::cout << numbers[0]; //garbage values
std::cout << &vowels[2]; //address of garbage valuesAccessing arrays
Of course, you can use indexes to get values for later use.
char vowels[5] = {'H', 'e', 'l', 'l', 'o'};
std::cout << vowels[0] << vowels[1] << vowels[2] << vowels[3] << vowels[4];
// The output will be: Hello
int numbers[] = {1, 2, 3, 4, 5};
int summ = numbers[0] + numbers[4] + numbers[2]; // The output will be: 9💡 Remember that arrays in C++ are zero-based, so the first element is accessed using index 0, the second element with index 1, and so on. The last element is accessed by the index equal to array size – 1;
int numbers[] = {1, 2, 3, 4, 5};
std::cout << numbers[1] << std::endl; //will be displayed 2 not 1
std::cout << numbers[0] << std::endl; //will be displayed 1
std::cout << numbers[4] << std::endl; //last element (5)
std::cout << numbers[5] << std::endl; //undefined behavior (potential error)When accessing elements in an array in C++, it is important to be cautious because C++ does not provide any built-in checks to ensure that you stay within the bounds of the array. It is your responsibility as the programmer to ensure that you do not access elements outside the valid range of the array. If you attempt to access elements beyond the array's bounds, you may encounter undefined behavior, which can result in unexpected program crashes or erroneous results. Therefore, it is crucial to carefully manage the indices used to access array elements and ensure they are within the valid range of the array.
Arrays and pointers
Arrays and pointers have a close relationship in C++. When you declare an array in C++, you are essentially creating a pointer that points to the first element of the array. Understanding this relationship enables array manipulation techniques.
Additionally, you can use pointer arithmetic to navigate through the array. Since arrays are contiguous blocks of memory, you can increment or decrement the pointer to access adjacent elements. For example:
int numbers[] = {1, 2, 3, 4, 5};
int *ptr = numbers; // Assign the pointer to the array
// Access the elements using pointer arithmetic
cout << *ptr << endl; // Output: value of numbers[0]
cout << *(ptr + 1) << endl; // Output: value of numbers[1]Understanding the relationship between arrays and pointers is essential in C++, as it allows you to work with arrays efficiently and perform operations using pointer arithmetic. For example, when passing an array to a function, instead of a large array, a pointer to the first element is passed, which increases speed.
Multi-dimensional arrays
C++ supports multi-dimensional arrays, which are arrays with multiple dimensions or dimensions of different sizes. They can be used to represent matrices, tables, or other structured data.
To declare and initialize a multi-dimensional array in C++, you specify the size of each dimension in the array declaration. Here's an example of a 2-dimensional array:
int matrix[3][4]; // Declare a 2-dimensional array of size 3x4In this case, the matrix array has 3 rows and 4 columns. You can access individual elements using two sets of subscripts. For example, matrix[0][0] refers to the element in the first row and first column.
You can also initialize a multi-dimensional array at the time of declaration. Here's an example:
int matrix[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};The length of an array and array size
An array has two important parameters: length and size. In C++, the length of an array refers to the number of elements it can hold, while the array size represents the amount of memory allocated for the array.
To determine the length of an array and its size, the already familiar sizeof() operator is used. The size of an array is the total amount of memory allocated for it, which includes all its elements (sizeof()returns the size of the array in bytes, not the number of elements). And the length of the array (number of elements) is the size of the array (in bytes) divided by the size of one element (in bytes). Here's some simple math 😎.
#include <iostream>
int main(){
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr);
std::cout << "Size of the array: " << size << std::endl; // Output: 20
int length = sizeof(arr) / sizeof(arr[0]);
std::cout << "Length of the array: " << length << std::endl; // Output: 5
return 0;
}Conclusion
In conclusion, arrays are fundamental data structures in C++ that allow us to store and manipulate collections of elements efficiently. They provide a convenient way to organize and access data in a structured manner. By understanding how to declare, initialize, and work with arrays, you can effectively manage and process data in your C++ programs.
Remember that arrays offer a way to organize and access data. Here are a few distinguishing features of an array in C++:
All elements are stored in the memory sequentially;
All elements in a collection are stored under the same name;
You can predetermine how many elements are in an array;
You can change the contents of the array at any time;
But you can't resize an array;
It's important to choose the appropriate array type based on your program's requirements.