Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages
Open In App
Next Article:
Advantages and Disadvantages of Array in C
Next article icon

Advantages and Disadvantages of Array in C

Last Updated : 02 Nov, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

An array is a data structure that stores a collection of elements, all of the same data type, in a contiguous block of memory. Each element in the array is identified by an index, which is a numerical value that represents the position of the element in the array. The first element in the array has an index of 0, the second element has an index of 1, and so on.

Arrays can be used to store a variety of data types, including integers, floats, characters, and even user-defined types. They are commonly used to store collections of similar data, such as a list of numbers, a list of strings, or a list of objects.

Though, the array got its own set of advantages and disadvantages.

Advantages of Arrays

Below are some advantages of the array:

  • In an array, accessing an element is very easy by using the index number.
  • The search process can be applied to an array easily.
  • 2D Array is used to represent matrices.
  • For any reason a user wishes to store multiple values of similar type then the Array can be used and utilized efficiently.
  • Arrays have low overhead.
  • C provides a set of built-in functions for manipulating arrays, such as sorting and searching.
  • C supports arrays of multiple dimensions, which can be useful for representing complex data structures like matrices.
  • Arrays can be easily converted to pointers, which allows for passing arrays to functions as arguments or returning arrays from functions.

Disadvantages of Arrays

Now let’s see some disadvantages of the array and how to overcome it:

Array size is fixed: The array is static, which means its size is always fixed. The memory which is allocated to it cannot be increased or decreased. Below is the program for the same:

C
// C program to illustrate that the // array size is fixed #include <stdio.h>  // Driver Code int main() {     int arr[10];      // Assign values to array     arr[0] = 5;     arr[5] = 6;     arr[7] = -9;      // Print array element at index 0     printf("Element at index 0"            " is %d\n",            arr[0]);      // Print array element at index 11     printf("Element at index 11"            " is %d",            arr[11]);      return 0; } 

Output
Element at index 0 is 5 Element at index 11 is -1176897384 

Explanation: In the above program the array of size 10 is declared and the value is assigned at a particular index. But when the value at index 11 is printed then it prints the garbage value because the array was accessed out of the bound index. In some compiler, it gives error as "Array Index Out Of Bound.".

How to overcome: To overcome that problem use Dynamic Memory Allocation like malloc(), calloc(). It also helps us to deallocates the memory using the free() method which helps to reduce wastage of memory by releasing it. Below is the program for the same:

C
// C program to illustrate the use of // array using Dynamic Memory Allocation #include <stdio.h> #include <stdlib.h>  // Driver Code int main() {     // Pointer will hold the base address     int* ptr;     int n = 10;      // Dynamically allocates memory     // using malloc() function     ptr = (int*)malloc(n * sizeof(int));      // Assign values to the array     for (int i = 0; i < n; i++) {         ptr[i] = i + 1;     }      // Print the array     printf("The elements are: ");      for (int i = 0; i < n; i++) {         printf("%d ", ptr[i]);     }      // Free the dynamically     // allocated memory     free(ptr);      return 0; } 

Output
The elements are: 1 2 3 4 5 6 7 8 9 10 

Array is homogeneous:The array is homogeneous, i.e., only one type of value can be store in the array. For example, if an array type "int", can only store integer elements and cannot allow the elements of other types such as double, float, char so on. Below is the program for the same:

C
// C++ program to illustrate that // the array is homogeneous #include <stdio.h>  // Driver Code int main() {     // Below declaration will give     // Compilation Error     int a[5] = { 0, 1, 2, "string", 9, 4.85 };      return 0; } 

Output
100 547.000000 Ram 

Output:

Explanation: The above code gives "Compilation Error" as an integer type array is assigned value to a string and float type.

How to overcome: To overcome that problem, the idea is to structure, where it can store non-homogeneous (heterogeneous) value. Below is the program for the same:

C
// C program to illustrate the use of // structure to store heterogeneous // variables #include <stdio.h>  // Structure students struct student {      int student_id;     float marks;     char name[30]; };  // Driver Code int main() {     // Structure variable s1     struct student s1 = { 100, 547, "Ram" };      // Accessing structure members     // using structure pointer     printf("%d\n", s1.student_id);     printf("%f\n", s1.marks);      for (int i = 0;          s1.name[i] != '\0'; i++) {         printf("%c", s1.name[i]);     }      return 0; } 

Output
100 547.000000 Ram 

Array is Contiguous blocks of memory: The array stores data in contiguous(one by one) memory location. Below is the representation of the same:

How to overcome: To overcome the sequential access to the array, the idea is to use the Linked list. In a Linked list, the elements are not stored in contiguous memory locations. Below is the representation of the same:

Insertion and deletion are not easy in Array: The operation insertion and deletion over an array are problematic as to insert or delete anywhere in the array, it is necessary to traverse the array and then shift the remaining elements as per the operation. This operation cost is more.

Example: For inserting 22 in 3rd position of the array then below are the steps:

Below is the program to illustrate the same:

C
// C Program to insert an element at // a specific position in an array #include <stdio.h>  // Driver Code int main() {     int arr[100] = { 0 };     int i, x, pos, n = 10;      // Initial array of size 10     for (i = 0; i < 10; i++) {         arr[i] = i + 1;     }      // Print the original array     for (i = 0; i < n; i++) {         printf("%d ", arr[i]);     }     printf("\n");      // Element to be inserted     x = 50;      // Position at which element     // is to be inserted     pos = 5;      printf("Array after inserting %d"            " at position %d\n",            x, pos);      // Increase the size by 1     n++;      // Shift elements forward     for (i = n - 1; i >= pos; i--) {          arr[i] = arr[i - 1];     }      // Insert x at pos     arr[pos - 1] = x;      // Print the updated array     for (i = 0; i < n; i++) {         printf("%d ", arr[i]);     }      return 0; } 

Output
1 2 3 4 5 6 7 8 9 10  Array after inserting 50 at position 5 1 2 3 4 50 5 6 7 8 9 10 

How to overcome: To overcome the above problem using a Linked List. Below is the representation of the same:

Below is the program to implement the same:

C
// C program to insert an element at // a position using linked list #include <stdio.h> #include <stdlib.h>  // Structure for the linked list struct node {     int data;     struct node* next; };  // head Node struct node* head;  // Function to insert any element // at the end of the linked list int insert_last(int k) {     struct node *ptr, *s;     ptr = (struct node*)         malloc(sizeof(struct node));     ptr->data = k;     ptr->next = NULL;      // If head is NULL     if (head == NULL) {         head = ptr;     }      // Else     else {          s = head;          // Traverse the LL to last         while (s->next != NULL) {             s = s->next;         }         s->next = ptr;     } }  // Function to display linked list void display() {     struct node* s;      // Store the head     s = head;      // Traverse till s is NULL     while (s != NULL) {          // Print the data         printf("%d ", s->data);         s = s->next;     }     printf("\n"); }  // Function to insert any element at // the given position of linked list int insert_position(int a, int b) {     int f = 0, i;     struct node *ptr, *s;      // Allocate new Node     ptr = (struct node*)         malloc(sizeof(struct node));     ptr->data = a;      // If first position     if (b == 1) {         ptr->next = head;         head = ptr;     }      // Otherwise     else {         s = head;          // Move to (b - 1) position         for (i = 0; i < b - 2; i++) {             s = s->next;         }          // Assign node         ptr->next = s->next;         s->next = ptr;     }      return 0; }  // Driver Code int main() {     // Given Linked List     insert_last(3);     insert_last(1);     insert_last(5);     insert_last(7);      printf("Current Linked List is: ");      // Display the LL     display();      // Insert 6 at position 4     insert_position(6, 4);     printf("\n Linked List after insert"            " 6 in 4th position: ");      // Display the LL     display();      return 0; } 

Output
Current Linked List is: 3 1 5 7    Linked List after insert 6 in 4th position: 3 1 5 6 7 

Next Article
Advantages and Disadvantages of Array in C

E

eshitadutta
Improve
Article Tags :
  • Linked List
  • C Programs
  • Data Structures
  • Difference Between
  • Articles
  • C Language
  • DSA
  • Arrays
  • cpp-structure
Practice Tags :
  • Arrays
  • Data Structures
  • Linked List

Similar Reads

    How to Initialize a 2D Array in C?
    In C, a 2D Array is a type of multidimensional array in which data is stored in tabular form (rows and columns). It has two dimensions so it can store the data and can expand in two directions. In this article, we will learn how to initialize a 2D array in C.There are three main ways to initialize t
    4 min read
    Return an Array in C
    In C, arrays are linear data structures that allow users to store the same data in consecutive memory locations. Returning an array in C can be a little complex because unlike C++, C does not support directly returning arrays from functions. In this article, we will learn how to return an array in C
    5 min read
    Array of Pointers to Strings in C
    In C, arrays are data structures that store data in contiguous memory locations. Pointers are variables that store the address of data variables. We can use an array of pointers to store the addresses of multiple elements. In this article, we will learn how to create an array of pointers to strings
    2 min read
    C Program to Remove All Occurrences of an Element in an Array
    To remove all the occurrences of an element in an array, we will use the following 2 approaches: Using MethodsWithout using methods or With Conditional Statement We will keep the same input in all the mentioned approaches and get an output accordingly. Input: array = {1, 2, 1, 3, 1} value = 1 Output
    3 min read
    Performance analysis of Row major and Column major order of iterating arrays in C
    In computing, row-major order and column-major order are methods for storing multidimensional arrays in linear storage such as random access memory.The two mentioned ways differ from each other with respect to the order in which elements are stored contiguously in the memory. The elements in row-maj
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences