- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# How do I find the length of an array in C/C++?

Some of the methods to find the length of an array are given as follows −

## Method 1 - Using sizeof operator

The sizeof() operator can be used to find the length of an array. A program that demonstrates the use of the sizeof operator in C++ is given as follows.

## Example

#include <iostream> using namespace std; int main() { int arr[5] = {4, 1, 8, 2, 9}; int len = sizeof(arr)/sizeof(arr[0]); cout << "The length of the array is: " << len; return 0; }

The output of the above program is as follows −

The length of the array is: 5

Now, let us understand the above program.

The variable len stores the length of the array. The length is calculated by finding size of array using sizeof and then dividing it by size of one element of the array. Then the value of len is displayed. The code snippet for this is given as follows −

int arr[5] = {4, 1, 8, 2, 9}; int len = sizeof(arr)/sizeof(arr[0]); cout << "The length of the array is: " << len;

## Method 2 - Using pointers

Pointer arithmetic can be used to find the length of an array. A program that demonstrates this is given as follows.

## Example

#include <iostream> using namespace std; int main() { int arr[5] = {5, 8, 1, 3, 6}; int len = *(&arr + 1) - arr; cout << "The length of the array is: " << len; return 0; }

## Output

The output of the above program is as follows −

The length of the array is: 5

Now, let us understand the above program.

The value contained in *(&arr + 1) is the address after 5 elements in the array. The value contained in arr is the address of the starting element in array. So their subtraction results in the length of the array. The code snippet for this is given as follows −

int arr[5] = {5, 8, 1, 3, 6}; int len = *(&arr + 1) - arr; cout << "The length of the array is: " << len;

- Related Questions & Answers
- How do you find the length of an array in C#?
- How to find the length of an array in JavaScript?
- How do you find the number of dimensions of an array in C#?
- How do I determine the size of my array in C#
- How do I empty an array in JavaScript?
- How do I get length of list of lists in Java?
- How do I get the average string length in MySQL?
- How do I reverse an int array in Java
- How do I find an element in Java List?
- How to find the length of the longest continuous increasing subsequence from an array of numbers using C#?
- How do I add a value to the top of an array in MongoDB?
- How Do I Find the Largest Number in a 3D JavaScript Array?
- How to find the length and rank of a jagged array in C#?
- How do I declare and initialize an array in Java?
- Find the Derangement of An Array in C++