Arrays in C/C++
An array is collection of items stored at continuous memory locations.

Why do we need arrays?
We can use normal variables (v1, v2, v3, ..) when we have small number of objects, but if we want to store large number of instances, it becomes difficult to manage them with normal variables. The idea of array is to represent many instances in one variable.
Array declaration in C/C++:

We can declare an array by specifying its type and size or by initializing it or by both.
- Array declaration by specifying size
// Array declaration by specifying sizeintarr1[10];// With recent C/C++ versions, we can also// declare an array of user specified sizeintn = 10;intarr2[n];chevron_rightfilter_none - Array declaration by initializing elements
// Array declaration by initializing elementsintarr[] = { 10, 20, 30, 40 }// Compiler creates an array of size 4.// above is same as "int arr[4] = {10, 20, 30, 40}"chevron_rightfilter_none - Array declaration by specifying size and initializing elements
// Array declaration by specifying size and initializing// elementsintarr[6] = { 10, 20, 30, 40 }// Compiler creates an array of size 6, initializes first// 4 elements as specified by user and rest two elements as 0.// above is same as "int arr[] = {10, 20, 30, 40, 0, 0}"chevron_rightfilter_none
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.
Following are few examples.
#include <stdio.h>intmain(){intarr[5];arr[0] = 5;arr[2] = -10;arr[3 / 2] = 2;// this is same as arr[1] = 2arr[3] = arr[0];printf("%d %d %d %d", arr[0], arr[1], arr[2], arr[3]);return0;}chevron_rightfilter_noneOutput:5 2 -10 5
- No Index Out of bound Checking:
There is no index out of bound checking in C, for example the following program compiles fine but may produce unexpected output when run.// This C program compiles fine// as index out of bound// is not checked in C.#include <stdio.h>intmain(){intarr[2];printf("%d ", arr[3]);printf("%d ", arr[-2]);return0;}chevron_rightfilter_noneOutput:2008101287 4195777
- Also, In C, it is not compiler error to initialize an array with more elements than specified size. For example the below program compiles fine and shows just Warning.
#include <stdio.h>intmain(){// Array declaration by initializing it with more// elements than specified size.intarr[2] = { 10, 20, 30, 40, 50 };return0;}chevron_rightfilter_noneWarnings:
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')
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]'” - An Example to show that array elements are stored at contiguous locations
// C program to demonstrate that array elements are stored// contiguous locations#include <stdio.h>intmain(){// an array of 10 integers. If arr[0] is stored at// address x, then arr[1] is stored at x + sizeof(int)// arr[2] is stored at x + sizeof(int) + sizeof(int)// and so on.intarr[5], i;printf("Size of integer in this compiler is %lu\n",sizeof(int));for(i = 0; i < 5; i++)// The use of '&' before a variable name, yields// address of variable.printf("Address arr[%d] is %p\n", i, &arr;[i]);return0;}chevron_rightfilter_noneOutput:Size of integer in this compiler is 4 Address arr[0] is 0x7ffd636b4260 Address arr[1] is 0x7ffd636b4264 Address arr[2] is 0x7ffd636b4268 Address arr[3] is 0x7ffd636b426c Address arr[4] is 0x7ffd636b4270
Array vs Pointers
Arrays and pointer are two different things (we can check by applying sizeof). The confusion happens because array name indicates 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++?
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.
Related Articles:
- Array is C/C++ | Set 2
- Initialization of multidimensional arrays in C/C++
- How to print size of array parameter in C++?
- Properties of arrays in C language
- Maximum element in an array
- Array sum in C++ STL
- Sort an array in C++
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Split the given array into K sub-arrays such that maximum sum of all sub arrays is minimum
- Merge k sorted arrays | Set 2 (Different Sized Arrays)
- Find sub-arrays from given two arrays such that they have equal sum
- Generate all possible sorted arrays from alternate elements of two given sorted arrays
- Maximum OR sum of sub-arrays of two different arrays
- Why is a[i] == i[a] in C/C++ arrays?
- Multidimensional Arrays in C / C++
- Minimum LCM and GCD possible among all possible sub-arrays
- Introduction to Arrays
- How to join two Arrays using STL in C++?
- C | Arrays | Question 11
- C | Arrays | Question 2
- C | Arrays | Question 1
- C | Arrays | Question 4
- C | Arrays | Question 10



