Get length of array in C and C++
Do not miss this exclusive book on Binary Tree Problems. Get it now for free.
Reading time: 20 minutes | Coding time: 5 minutes
Unlike other languages where array is defined by the starting memory address, datatype and the length of the array, in C, array is a similar pointer to a memory location which is the starting memory address. We need to use the sizeof operator in C/ C++ to achieve this.
The key idea of getting the length of an array in C or C++ is:
int length = sizeof(array)/sizeof(<data_type of the array>);
// length = total size of array / size of an array element
For Integer array, we can find the length as follows:
int length = sizeof(array)/sizeof(int);
This works with array of user defined objects (in C++) and struct (in C and C++) as well. We will demonstrate this on the following examples:
- Array of Integer
- Array of Character
- Array of Struct
- Array of user defined object (in C++)
We will go through each example in detail.
Array of Integer
In this example, we will understand how to get the length of an array of integers. We defined an array of integers like this:
int array[] = {1, 3, 2};
Following this, we found the length of the array as follows:
int length = sizeof(array) / sizeof(int);
Following is the complete C example:
// Part of OpenGenus IQ
#include<stdio.h>
int main()
{
// define sample array
int array[] = {1, 3, 2};
// get length of array as explained by OpenGenus
int length = sizeof(array) / sizeof(int);
// print length of array
printf("Length of array is %d\n", length);
return 0;
}
Output:
Length of array is 3
Array of Character
In this example, we will understand how to get the length of an array of characters. We defined an array of characters like this:
char array[] = {'o', 'p', 'e', 'n', 'g', 'e', 'n', 'u', 's'};
Following this, we found the length of the array as follows:
int length = sizeof(array) / sizeof(char);
Following is the complete C example:
// Part of OpenGenus IQ
#include<stdio.h>
int main()
{
// define sample array
char array[] = {'o', 'p', 'e', 'n', 'g', 'e', 'n', 'u', 's'};
// get length of array as explained by OpenGenus
int length = sizeof(array) / sizeof(char);
// print length of array
printf("Length of array is %d\n", length);
return 0;
}
Output:
Length of array is 9
Array of struct
In this example, we will understand how to get the length of an array of struct. We defined a struct like this:
struct opengenus
{
int data;
float weigth;
};
We used the above struct to get an array of struct:
struct opengenus array[5];
Following this, we found the length of the array as follows:
int length = sizeof(array) / sizeof(struct opengenus);
Following is the complete C example:
// Part of OpenGenus IQ
#include<stdio.h>
//define a struct
struct opengenus
{
int data;
float weigth;
};
int main()
{
// define sample array
struct opengenus array[5];
// fill in the array
for(int i = 4; i>=0; i--)
{
array[i].data = i;
array[i].weigth = i*2+0.1;
}
// get length of array as explained by OpenGenus
int length = sizeof(array) / sizeof(struct opengenus);
// print length of array
printf("Length of array is %d\n", length);
return 0;
}
Output:
Length of array is 5
Array of User defined object in C++
In this example, we will understand how to get the length of an array of user defined objects. We defined a user defined class like this:
class opengenus
{
public:
double data;
double weight;
};
We used the above class to get an array of user defined objects:
opengenus array[6];
Following this, we found the length of the array as follows:
int length = sizeof(array) / sizeof(opengenus);
Following is the complete C example:
// Part of OpenGenus IQ
#include <iostream>
using namespace std;
//define a class
class opengenus
{
public:
double data;
double weight;
};
int main()
{
// define sample array
opengenus array[6];
// fill in the array
for(int i = 5; i>=0; i--)
{
array[i].data = i;
array[i].weight = i*2+0.1;
}
// get length of array as explained by OpenGenus
int length = sizeof(array) / sizeof(opengenus);
// print length of array
cout << "Length of array is " << length;
return 0;
}
Output:
Length of array is 6
With this, you have the complete idea of getting the length of any array in C and C++ using sizeof operator. Enjoy.
Sign up for FREE 3 months of Amazon Music. YOU MUST NOT MISS.