what does
sizeof(arr) and sizeof(int) return
and why is int n = sizeof(arr)/sizeof(int) used
what does
sizeof(arr) and sizeof(int) return
and why is int n = sizeof(arr)/sizeof(int) used
@Souradeep-Kundu-824545687968212
sizeof operator gives the size in memory taken by that variable or type.
int arr[] = {10,20,30,40,50 };
cout << sizeof(arr) << endl ; // Gives 5*8 = 40
Assume a system takes 8 bytes for a single int . arr has 5 integers . So it will take 40 bytes in total.
cout << sizeof(int) << endl; // Gives 8
Now we can obtain the no of elements in the above array as
int n = sizeof(arr) / sizeof(int) ; // 40 / 8 = 5
cout << n << endl ; // 5