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 Find the Mode of Numbers in an Array in C++?
Next article icon

How to Find the Length of an Array in C++?

Last Updated : 29 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In C++, the length of an array is defined as the total number of elements present in the array. In this article, we will learn how to find the length of an array in C++.

The simplest way to find the length of an array is by using the sizeof operator. First calculate the size of the array in bytes and divide it by the size of the first element to get the length of the array. Let's take a look at an example:

C++
#include <bits/stdc++.h> using namespace std;  int main() {     int arr[] = {1, 2, 3, 4, 5};      // Finding the length of the array     int n = sizeof(arr) / sizeof(arr[0]);      cout << n;     return 0; } 

Output
5

Note: Any of the method mentioned here cannot find the size of those array that are passed to the functions due to array decay in C++. That is why it is recommended to store and pass the size of the array using some variable.

Using Pointer Arithmetic

First get the pointer to the array by using & addressof operator on the array name and increment this pointer. Deference the obtained pointer and subtract the pointer to the first element of the array to get the size of the array.

C++
#include <bits/stdc++.h> using namespace std;  int main() {     int arr[5] = {1, 2, 3, 4, 5};      // Finding length of an array by using      // pointer arithmetic     cout << *(&arr + 1) - arr;      return 0; } 

Output
5

Explanation: This trick works because of pointer arithmetic in C++. When an integer is added to a pointer, the compiler adjusts the address by multiplying the integer by the size of the data type the pointer points to.

In this case, we took a pointer to a given array arr and added 1 to it. When we add 1 to a pointer of an array, the compiler moves the pointer by the size of one whole array (not element). Subtracting the pointer to the first element from this pointer gives the array's length.

Using size() (Since C++17)

C++17 introduced the size() function, which can be used to get the number of elements in an array.

C++
#include <bits/stdc++.h> using namespace std;  int main() {     int arr[] = {1, 2, 3, 4, 5};      	// Finding the size of array arr     int n = size(arr);        cout << n;     return 0; } 


Output

5



Next Article
How to Find the Mode of Numbers in an Array in C++?

A

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

Similar Reads

  • How to Find the Median of Array in C++?
    In C++, the array is a collection of elements of the same type, In this article, we will learn how to find the median of the array in C++. The median of the array will be the middle element if the number of elements is odd or the average of two middle elements if the number of elements is even in th
    2 min read
  • How to Find the Length of a Substring in a char Array?
    In C++, a substring is a string inside another string that is the contiguous part of the string. In this article, we will learn how to find the length of a substring in a char array in C++. Example Input: str[] = "Hello, World!";substr= "World";Output: Length of substring World is: 5Finding Length o
    2 min read
  • How to Find the Mode in a 2D Array in C++?
    A mode is a number that occurs most frequently in comparison to other numbers in a given dataset. In this article, we will find the mode in a 2D array of integers in C++. Input:myArray = { {1, 2, 2, 3}, {3, 4, 5, 5}, {5, 6, 7, 8} }Output: 5Mode in 2D ArrayTo find a mode of numbers in a 2D array, we
    2 min read
  • How to Find the Median of a Sorted Array in C++?
    In C++, the median of a sorted array is the middle element if the array size is odd, or the average of the two middle elements if the array size is even. In this article, we will learn how to find the median of a sorted array in C++. Example Input: myArray: {1, 2, 3, 4, 5} Output: Median of the Arra
    2 min read
  • How to Find the Mode of Numbers in an Array in C++?
    Mode of any dataset is the item that occurs most frequently in it. In this article, we will find the mode of numbers in an unsorted array in C++. For Example,Input: myArray = { 1, 2, 3, 4, 5, 2, 3, 2, 2, 4, 2 } Output: Mode : 2Finding Mode of Array Elements in C++To find the mode of the array elemen
    2 min read
  • How to Find the Sum of Elements in an Array in C++?
    In C++, an array is the collection of similar data elements that are stored in the contiguous memory location and we can access these elements directly by their index value. In this article, we will learn how to find the sum of elements in an array in C++. Example:Input: myVector = {1, 2, 3, 4, 5} O
    2 min read
  • How to Find the Mode in a Sorted Array in C++?
    The mode of the given numbers can be defined as the value that occurs the most in the given dataset or the value with the highest frequency. In this article, we will discuss how to calculate the mode of the numbers in a sorted array in C++. Example: Input: myVector = {1, 2, 2, 3, 3, 3, 4, 4, 5} Outp
    3 min read
  • How to Find the Range of Numbers in an Array in C++?
    The range of numbers in an array means the difference between the maximum value and the minimum value in the given array. In this article, we will learn how to find the range of numbers in an array in C++. For Example, Input: myArray = {5,10,15,30,25}; Output: Range: 25Range Within an Array in C++To
    2 min read
  • How to Find the Size of a List in C++?
    In C++, Standard Template Library (STL) we have a std::list container that is a doubly-linked list in which elements are stored in non-contiguous memory allocation. In this article, we will learn how to find the size of a list in C++ STL. Example: Input: myList = {10, 20, 30, 40, 50}; Output: Size o
    2 min read
  • How to Find the Smallest Number in an Array in C++?
    In C++, arrays are the data types that store the collection of the elements of other data types such as int, float, etc. In this article, we will learn how to find the smallest number in an array using C++. For Example,Input: myVector = {10, 3, 10, 7, 1, 5, 4} Output: Smallest Number = 1Find the Sma
    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