How to find the size of an int[] in C/C++?

CC++Server Side ProgrammingProgramming

In this section, we will see how we can get the size of an integer array in C or C++? The size of int[] is basically counting the number of elements inside that array. To get this we can use the sizeof() operator. If the array name is passed inside the sizeof(), then it will return total size of memory blocks that are occupied by the array. Now if we divide it by the size of each element, then we can get number of elements.

Let us see the following example to get the better idea about it.

Example

#include <iostream>
using namespace std;
int main() {
   int data[] = {11, 22, 33, 44, 55, 66, 77, 88, 99, 91, 82, 73, 64};
   cout << "Memory occupied by data[]: " << sizeof(data) << endl;
   cout << "Size of data[] array: " << sizeof(data)/sizeof(data[0]) << endl;
}

Output

Memory occupied by data[]: 52
Size of data[] array: 13
raja
Published on 02-May-2019 08:08:42
Advertisements