Arrat in C

Array in C| All You Need To Know

Introduction To Array in C

An array in c is a collection of items of the same data type stored at contiguous memory locations. This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of the array (generally denoted by the name of the array).

For simplicity, we can think of an array as a fleet of stairs where on each step is placed a value (let’s say one of your friends). Here, you can identify the location of any of your friends by simply knowing the count of the step they are on.

Remember: “Location of next index depends on the data type we use

Array By GeeksToCode
Arrat in C

Defining an Array in C:

Array’s definition is similar to defining any other variable. There are two things need to be kept in mind, the data type of the array elements and the size of the array. The size of the array is fixed and the memory for an array needs to be allocated before use, the size of an array cannot be increased or decreased dynamically.

                                                                 ARRAY OF INTEGER’S

          3         4        6         8         9   1     10

   200               202            204                 206             208              210             212

Declaring Array             

Data Type   arrayName[size of array];

Initializing Array            –     

   Data Type arrayName[size of array] = {values separated by comma};

Accessing array elements:

Arrays allow accessing elements randomly. Elements in an array can be accessed using indexes. Suppose an array named arr stores N elements. Indexes in an array are in the range of 0 to N-1, where the first element is present at 0-th index and consecutive elements are placed at consecutive indexes. Element present at ith index in the array arr[] can be accessed as arr[i].

using namespace std;
int main()
    int arr[10]= {1,2,3,4,5,6,9,7,8,10};  // Declaring an array of Integers // Initializing array
    for(int i = 0;i<10;i++)
    cout<<arr[i]<<" ";
    return 0;

Output - 1 2 3 4 5 6 9 7 8 10
using namespace std;
int main()
    char arr[5]= {'a','g','h','i','j'};  // Declaring an array of Character // Initializing array
    for(int i = 0;i<5;i++)
    cout<<arr[i]<<" ";
    return 0;
Output a g h i j

Memory Assigning in Array

Memory assigned in array in linear fashion Here is a simple example

Note – 1 When you print the name of the array, you indirectly accessing the first element of the array

using namespace std;
int main()
    int arr[10]= {1,2,3,4,5,6,9,7,8,10};  
    for(int i = 0;i<10;i++)
    cout<<arr+i<<" ";
    return 0;

When you run this program, You will see the address of each element of the array is incremented by 4. Here 4 is the size of Integer

So, we can conclude that the next index of the array have the address of previous index + size of data type used

Address of index 1 = Address of index 0 + size of (data type);

Do the same for character array and see the result

Dynamic Memory Allocation

We have now seen how to allocate memory for an array in a fixed size. This means that the array is of static type. Hence more memory is being wasted and this leads to poor space allocation. So it is better to allocate the memory size according to the user input. Is that even possible? YES! We use a concept called dynamic memory allocation.

Let’s see the syntax and example.


pointer-variable = new data-type;

Let’s see an example to understand it clearly.

 int *p = new int[10]

This is how you allocate the memory of the array. Let’s now see another example of how to get memory size from the user and allocate the memory.

    #include <iostream.h>
    int main()
     int n;
     int p=NULL
     cout<<"Give the array size:";     cin>>n;
     int *p = new int[n];
     return 0;

Advantages of using arrays:

Arrays allow random access of elements. This makes accessing elements by position faster.

Arrays have better cache locality that can make a pretty big difference in performance.

  1. Arrays represent multiple data items of the same type using a single name.
  2. The arrays allocate memory in contiguous memory locations for all its elements. Hence there is no chance of extra memory being allocated in the case of arrays. This avoids memory overflow or shortage of memory in arrays.
  3. Using arrays, other data structures like linked lists, stacks, queues, trees, graphs, etc can be implemented.
  4. Two-dimensional arrays are used to represent matrices.

Disadvantages of Arrays

The number of elements to be stored in an array should be known in advance.

An array is a static structure (which means the array is of fixed size). …

Insertion and deletion are quite difficult in an array as the elements are stored in consecutive memory locations and the shifting operation is costly.

Allocating more memory than the requirement leads to wastage of memory space and less allocation of memory also leads to a problem.

Topics To Be Discussed

  1. Searching
  2. Sorting
  3. Sliding Window Technique
  4. Prefix Sum Technique
  5. Applications of array in real life
Sudhanshu is Technology geek and also a pro pubg player. I like to create content in my free time that helps others. Currently pursuing BCA from Noida and operating Geekstocode

Leave a Comment

Your email address will not be published. Required fields are marked *