Arrays in C/C++
An array in C/C++ or be it in any programming language is a collection of similar data items stored at contiguous memory locations and elements can be accessed randomly using indices of an array. They can be used to store collection of primitive data types such as int, float, double, char, etc of any particular type. To add to it, an array in C/C++ can store derived data types such as the structures, pointers etc. Given below is the picture representation of an array.
Why do we need arrays?
We can use normal variables (v1, v2, v3, ..) when we have a small number of objects, but if we want to store a large number of instances, it becomes difficult to manage them with normal variables. The idea of an array is to represent many instances in one variable.
Array declaration in C/C++:
Note: In above image int a[3]={[0…1]=3}; this kind of declaration has been obsolete since GCC 2.5
There are various ways in which we can declare an array. It can be done by specifying its type and size, by initializing it or both.
Array declaration by specifying size
- C
Array declaration by initializing elements
- C
Array declaration by specifying size and initializing elements
- C
Advantages of an Array in C/C++:
- Random access of elements using array index.
- Use of less line of code as it creates a single array of multiple elements.
- Easy access to all the elements.
- Traversal through the array becomes easy using a single loop.
- Sorting becomes easy as it can be accomplished by writing less line of code.
Disadvantages of an Array in C/C++:
- Allows a fixed number of elements to be entered which is decided at the time of declaration. Unlike a linked list, an array in C is not dynamic.
- Insertion and deletion of elements can be costly since the elements are needed to be managed in accordance with the new memory allocation.
Facts about Array in C/C++:
- Accessing Array Elements:
Array elements are accessed by using an integer index. Array index starts with 0 and goes till size of array minus 1. - Name of the array is also a pointer to the first element of array.
Example:
- C
- C++
Output
5 2 -10 5
No Index Out of bound Checking:
There is no index out of bounds checking in C/C++, for example, the following program compiles fine but may produce unexpected output when run.
- C
- C++
-449684907 4195777
In C, it is not a compiler error to initialize an array with more elements than the specified size. For example, the below program compiles fine and shows just Warning.
- C
Warnings:
prog.c: In function 'main': prog.c:7:25: warning: excess elements in array initializer int arr[2] = { 10, 20, 30, 40, 50 }; ^ prog.c:7:25: note: (near initialization for 'arr') prog.c:7:29: warning: excess elements in array initializer int arr[2] = { 10, 20, 30, 40, 50 }; ^ prog.c:7:29: note: (near initialization for 'arr') prog.c:7:33: warning: excess elements in array initializer int arr[2] = { 10, 20, 30, 40, 50 }; ^ prog.c:7:33: note: (near initialization for 'arr')
- Note: The program won’t compile in C++. If we save the above program as a .cpp, the program generates compiler error “error: too many initializers for ‘int [2]'”.
The elements are stored at contiguous memory locations
Example:
- C
- C++
Size of integer in this compiler is 4 Address arr[0] is 0x7ffe75c32210 Address arr[1] is 0x7ffe75c32214 Address arr[2] is 0x7ffe75c32218 Address arr[3] is 0x7ffe75c3221c Address arr[4] is 0x7ffe75c32220
Another way to traverse the array
- C++
11 12 13 14 15 16 By Other Method: 11 12 13 14 15 16
Array vs Pointers
Arrays and pointer are two different things (we can check by applying sizeof). The confusion happens because array name indicates the address of first element and arrays are always passed as pointers (even if we use square bracket). Please see Difference between pointer and array in C? for more details.
What is vector in C++?
A vector in C++ is a class in STL that represents an array. The advantages of vector over normal arrays are,
- We do not need pass size as an extra parameter when we declare a vector i.e, Vectors support dynamic sizes (we do not have to initially specify size of a vector). We can also resize a vector.
- Vectors have many in-built function like, removing an element, etc.
To know more about functionalities provided by vector, please refer vector in C++ for more details.
if you know more about this articles click on this button