Introduction of Array Data Structure

By | February 21, 2024

Array is collection of items having same data type stored in contiguous memory location. An array is derived data type in C programming language which can store similar types of data in contiguous memory locations. Data may be primitive type, (i.e., int, char, float, double), address of union, structure, pointer, function or another array.

Array implementation is important because: most assembly languages have no concept of arrays, or from an array any other data structure we might want, can be built.

Example :

int arr[10] ;
char arr[10] ;
float arr[10] ;
long double arr[10] ;
char * char[10] ;
int (arr[]) () ;
double ** arr[10] ; 

Properties of Array :

  1. It is collection of items having same data type, i.e., each element is of the same size.
  2. Array size must be a constant expression and not a variable.
  3. It stores data in contiguous memory location.
  4. Starting address of array is the base address of array.
  5. Two-dimensional array elements are stored row by row in subsequent memory locations.
  6. Array size should be mentioned in the declaration.

Indexing in array :
Note that location of next index depends on the data type we use, in general case.

Array Indexing

Indexing of array is method of starting index of array. There are three types of indexing can be used in array.

1. 0 (zero-based indexing)
1. 1 (one-based indexing)
1. n (n-based indexing) 

Note – In C, index of array starts from 0 by default.

Array declaration in C/C++ :
Array declaration can be done by specifying its type and size, by initializing it or both.

  1. declaration by specifying size :
    // Array declaration by specifying size 
    int arr[5]; 
  2. initialize after declaration :
    // initialize after declaration
    int n = 10; 
    int arr[n];  
  3. Declaration by initializing elements :
    // declaration and initializing array 
    int arr[] = { 10, 30, 20, 40 }  
  4. Declaration by size and initializing elements :
    // declaration and initializing array 
    int arr[4] = { 10, 30, 20, 40 }  

Example of array in C :

#include <stdio.h> 

int main() 
{ 
    int arr[4]; 
    arr[0] = -5; 
    arr[1] = 20; 

    // same as arr[1] = 2 
    arr[3 / 2] = 30; 

    arr[3];
    arr[4] = arr[0]; 
  
    printf("%d %d %d %d", arr[1], arr[2], arr[0], arr[3]); 
  
    return 0; 
} 

Output:

20 30 -5 garbage_value

Advantages of using arrays:

  • There is random access possible, so accessing elements by position faster.
  • Faster performance because of better cache locality
  • We can access any element easily.
  • Only one loop required to access all elements of array.
  • Binary search is possible if array has sorted integer elements.
  • Sorting in the array data structure is easy.

Disadvantages of using arrays:

  • It is has fixed number of blocks.
  • Unlike a linked list, an array in C is not dynamic.
  • Insertion and deletion is costly unlike in linked list.

You can watch this good video about this topic –

Please write comments if you find anything incorrect. A gentle request to share this topic on your social media profile.

Author: Mithlesh Upadhyay

I hold an M.Tech degree in Artificial Intelligence (2023) from Delhi Technological University (DTU) and possess over 4 years of experience. I worked at GeeksforGeeks, leading teams and managing content, including GATE CS, Test Series, Placements, C, and C++. I've also contributed technical content to companies like MarsDev, Tutorialspoint, StudyTonight, TutorialCup, and Guru99. My skill set includes coding, Data Structures and Algorithms (DSA), and Object-Oriented Programming (OOPs). I'm proficient in C++, Python, JavaScript, HTML, CSS, Bootstrap, React.js, Node.js, MongoDB, Django, and Data Science.