Skip to content
geeksforgeeks
  • 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
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
Open In App
Next Article:
How to Check if a Deque is Empty in C++?
Next article icon

How to Check if an Array is Empty in C++?

Last Updated : 18 Mar, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In C++, arrays are fixed-size data structures that allow the users to store similar data in contiguous memory locations. In this article, we will learn how to check if an array is empty in C++.

Example:

Input:
arr[]={}
arr[]={1,2,3,4,5}

Output:
The array is empty
The array is not empty

Check if an Array is Empty

In C++, there is no direct way to check if array is empty or not. Arrays in C++ hold the assigned amount of memory and this memory will contain some garbage values even when we don't assign any valid value to the array elements. Also, there is no indicator that tells whether the given value is assigned by the user or it is a garbage value.

But we can have some methods to track the values count which can help in checking if the array is empty. One is maintaining a pointer that keep the track of the location of the last element present in the array. Other one is using a sentinal node to represent the end of the array.

The below example shows how the tracking of last element location helps in checking

C++ Program to Check if an Array is Empty

The below program illustrates how we can check if an array is empty or not in C++.

C++
// C++ Program to illustrate how to check if an array is // empty #include <iostream> using namespace std;  // Define a class named Array class Array {     int* arr; // Pointer to the first element of the array     int* end; // Pointer to the last element of the array     int capacity; // Size of the array  public:     // Constructor that initializes the array, end pointer     // and capacity     Array(int size)     {         arr = new int[size];         end = arr;         capacity = size;     }      // Function to insert an element at the end of the array     void insert(int val)     {         if (end - arr < capacity) {             *end = val;             end++;         }         else {             cout << "Array is full.\n";         }     }      // Function to check if the array is empty     bool isEmpty() { return end == arr; } };  int main() {     // Create an array of size 5     Array a(5);      // Check if the array is empty and print a message     // accordingly     if (a.isEmpty()) {         cout << "Array is empty.\n";     }     else {         cout << "Array is not empty.\n";     }      return 0; } 

Output
Array is empty.  

Time Complexity: O(1)
Auxiliary Space: O(1)




Next Article
How to Check if a Deque is Empty in C++?

N

nikitamehrotra99
Improve
Article Tags :
  • C++ Programs
  • C++
  • cpp-array
  • CPP Examples
Practice Tags :
  • CPP

Similar Reads

  • How to Check if a Map is Empty in C++?
    In C++, a map is an associative container that stores elements as key-value pairs and an empty map means it contains no elements. In this article, we will learn how to check if a map is empty or not in C++. Example: Input: map<int,string>mp1 = {{1, "Ram"}, {2, "Mohit"}};map<int,string> m
    2 min read
  • How to Check if a Set is Empty in C++?
    In C++, a set is an associative container that stores unique elements in a sorted order. In this article, we'll explore different approaches to check if a set is empty in C++ STL. Check if a Set is Empty or Not in C++To check if a std::set is empty in C++, we can use the std::set::empty() function.
    2 min read
  • How to Check if a List is Empty in C++?
    In C++, a list is a sequence container that allows non-contiguous memory allocation and is implemented using a doubly linked list. In this article, we will learn how to check if a list is empty in C++. Example: Input: myList = {1, 2, 3}; Output: List is not empty.Check if a List is Empty in C++To ch
    2 min read
  • How to Check if a Stack is Empty in C++?
    In C++, we have a stack data structure that follows a LIFO (Last In First Out) rule of operation. In this article, we will learn how to check if a stack is empty in C++. Example:Input:myStack = {1, 2, 3 } Output:Stack is not EmptyChecking if a Stack is Empty in C++To check if a stack is empty in C++
    2 min read
  • How to Check if a Deque is Empty in C++?
    In C++, a deque is a container provided by the STL library that is similar to a queue. However, unlike queues, it allows insertion and deletion from both ends. In this article, we will learn how to determine whether a deque is empty or not in C++. Example: Input: myDeque = {2, 4, 6 } Output: dq1 is
    2 min read
  • How to Check if a String is Empty in C++?
    In C++, strings are the sequence of characters that are stored as std::string class objects. In this article, we will learn how to check if a string is empty in C++ Example Input: str1="Hello! Geek" ; str2="" Output: str1 is not empty str2 is emptyChecking if the String is Empty in C++To check for a
    2 min read
  • How to Check if a Vector is Empty in C++?
    A vector is said to be empty when there are no elements present in vector. In this article, we will learn different ways to check whether a vector is empty or not. The most efficient way to check if the vector is empty or not is by using the vector empty() function. Let’s take a look at a simple exa
    2 min read
  • How to Empty an Array in C++?
    In C++, arrays are fixed-size containers that store elements of the same data type. Empty an array means removing all elements from it. In this article, we will see how to empty an array in C++. Example: Input: myArray = { 1, 8, 2, 9, 1, 5, 6 } Output: myArray = { 0, 0, 0, 0, 0, 0, 0 }Clear an Array
    2 min read
  • How to Declare an Array in C++?
    In C++, an array is a collection of similar data types in which elements are stored in contiguous memory locations. In this article, we will learn how to declare an array in C++. Declaring an Array in C++In C++, we can declare an array by specifying the type of its elements, followed by the name of
    2 min read
  • How to Check if a Substring Exists in a Char Array in C++?
    In C++, Char array and std::string both are used to store a sequence of characters to represent textual data. In this article, we will learn how to check if a substring exists within a char array in C++. Example Input: charArray[]= "Hello, Geek" subString="Geek" Output: Geek substring is found in ch
    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