
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find size of array in C/C++ without using sizeof
In this program, we find out Find size of array in C/C++ without using sizeof.
Algorithm
Begin Initialize the elements of the array. &a => This is the pointer to array which points at the same memory address as a. &a + 1 => It points at the address after the end of the array. *(a+1) => Dereferencing to *(&a + 1) gives the address after the end of the last element. *(a+1)-a => Subtract the pointer to the first element to get the length of the array. Print the size. End.
Example Code
#include <iostream> using namespace std; int main() { int a[] = {6,7,5,3,1,4,2,10,9}; int s = *(&a + 1) - a; cout << "Number of elements in array is "<< s; }
Output
Number of elements in array is 9
- Related Articles
- How to find the size of a variable without using sizeof in C#?
- Result of sizeof operator using C++
- Find maximum in an array without using Relational Operators in C++
- Find minimum in an array without using Relational Operators in C++
- How to use sizeof() operator to find the size of a data type or a variable in C#
- Find largest element from array without using conditional operator in C++
- Find the only repeating element in a sorted array of size n using C++
- Implement your own sizeof operator using C++
- Sizeof operator in C
- How to define an array size in java without hardcoding?
- Find array using different XORs of elements in groups of size 4 in Java
- Find Duplicates of array using bit array in C++
- Why is not sizeof for a struct equal to the sum of sizeof of each member in C/C++?
- Find Maximum XOR value of a sub-array of size k in C++
- Find ceil of a/b without using ceil() function in C++.

Advertisements