What is an array in C/C++? – Basic Guide to C++

What is an array

An array in C or C++ is defined as the collection of homogeneous or similar items that are stored in consecutively memory locations. In simple words, arrays are used to store similar types of data.

These similar types of data are related to the 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 programming, arrays are used.

But why Arrays?

The main reason lies in the execution speed. Arrays are stored in such consecutive memory locations that can easily be iterated and perform the operations. Due to their fixed size and position, arrays beat almost other data structures like linked-list or trees. Another reason is due to its simple syntax, which is similar to declaring a variable.

When compared to linked-list or trees, you need to write long code, declaring pointers and assigning location references. Without declaring various variables, you can declare an array, store the values in it and access it or modify the elements.

How do Arrays work?

As discussed earlier, it is a simple declaration like a variable. Checkout 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  

what is an array in c/c++? - basic guide to c++

 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 dimension 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 ]



3D Array :  The elements are stored in a 3D- Cubical like Structure, the syntax is

         int arr[3][3][3];  // for 3-D array

what is an array in c/c++? - basic guide to c++

Disadvantages of using arrays

Arrays are declared in a fixed size, so either element cannot be added more than its defined size and also large array size with few elements is a waste of memory. It is not dynamic in nature.

The time taken for iteration becomes large as the size of the array is increased.

Important operations like Insertion and Deletion takes more time as it involves searching and moving all the elements, hence Linked-List is preferred for such case.

Show More

Related Articles

Leave a Reply

Back to top button