What is an array in C/C++? – Basic Guide to C++
An array in C or C++ is a collection of homogeneous or similar items stored in consecutive memory locations. In simple words, arrays are used to store similar types of data.
These similar types of data are related to primitive data types such as long, int, float, char, etc. And not only related to this, but you can also even define your own data-type object or derived datatype using structure in C/C++ and store them in a continuous fashion way. If you come across programming, then the array is a very useful data structure. Even in large, competitive programmes, arrays are used.
But why Arrays?
The main reason lies in the execution speed. Arrays are stored in consecutive memory locations that can easily be iterated and performed. Due to their fixed size and position, arrays beat almost all other data structures like linked lists or trees. Another reason is due to its simple syntax, which is similar to declaring a variable.
Compared to linked lists or trees, you need to write long code, declare pointers, and assign location references. Without declaring various variables, you can declare an array, store its values, and access it or modify the elements.
How do Arrays work?
As discussed earlier, it is a simple declaration like a variable. Check out this syntax on your IDE.
Syntax for array in C/C++: int arr[5] = {0, 1, 2, 3, 4};
It can also be initialized as : int arr[5];
And later we can define elements
And also it can be defined as: int arr[] = {1,2,3,4,5};
Here size is taken automatically
You can check memory location using & operator. Example: &arr [ 1 ] = gives address
The index is used to find the location of elements inside an array. It generally starts with 0, 1, 2, 3, … , size-1.
If you declare array of double or float type, then it consist of element with double or floating values. For example:
double arr[5] = {0.01, 0.02, 0.03, 0.04, 0.05};
Types of Arrays: Array can be classified into Dimensions. The array can be a single-dimensional or linear array. Double-Dimensional Array or 2D array, Three-Dimensional Array or 3D Array and Multi-Dimensional Arrays. For example,
1D Array : [ 1, 2, 3, 4, 5 ]
2D Array : Example: int arr[3][3] =
[
[ 1, 2, 3, 4 ],
[ 5, 6, 7, 8 ],
[ 9, 10, 11, 12 ],
[ 13, 14, 15, 16 ]
]
1 | 2 | 3 | 4 |
5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 |
3D Array : The elements are stored in a 3D- Cubical like Structure, the syntax is
int arr[3][3][3]; // for 3-D array
Disadvantages of using arrays
One of the main disadvantages of using arrays is that they are fixed in size. This means that you cannot change the size once you create an array. If you need to store more data than what the array can hold, you will need to create a new array. This can be a waste of time and memory. Another disadvantage of arrays is that they are not very flexible. You can only store data of the same type in an array. This can be a problem if you need to store data of different types. For example, you may need to store an integer, a string, and a float in the same array. This is not possible with an array.
Arrays are also not very efficient when it comes to insertions and deletions. If you need to insert or delete an element in the middle of an array, you will need to shift all the elements after the insert/delete point. This can be time-consuming. Finally, arrays are not easy to print. If you need to print all the elements of an array, you will need to write a loop that iterates through the array and prints each element. This can be a bit cumbersome.
Overall, there are some disadvantages to using arrays in C++. However, they are still a useful data structure and can be used in many situations.