0

How to find the size of an array...using sizeof operator?

13th May 2020, 3:56 PM
pooja sharma
pooja sharma - avatar
7 Respuestas
+ 2
Martin Taylor rightly said but all I had to do was to answer the question. As you mentioned it is really pointless to find the size of the array when it is already known to you. But wouldn't it be helpful in this case- int arr[ ] = {1,2,3,4,5,6,...}; Let us say the user did not know how many elements he/she would enter into the array, in that case wouldn't it make sense to use the sizeof() to find the array length? I do not practice this language at all so probably I have forgot a few things but surely have learnt about the sizeof() from you today. Thanks.
13th May 2020, 9:06 PM
Avinesh
Avinesh - avatar
+ 2
int arr[10]; int size = sizeof(arr)/sizeof(arr[0]);
13th May 2020, 4:15 PM
Avinesh
Avinesh - avatar
+ 2
pooja sharma lets say integer data type takes 4 bytes in RAM. When u have an array of 3 integers, 12 bytes of memory will be reserved for that array, so if you divide 12 bytes (whole memory of array) by 4 bytes (memory that 1 element in array occupies) you get 3 which is the size of array.
13th May 2020, 4:37 PM
Mustafa K.
Mustafa K. - avatar
+ 1
pooja sharma you simply divide the amount of memory taken by whole array by an element inside
13th May 2020, 4:25 PM
Mustafa K.
Mustafa K. - avatar
0
But how sizeof(arr[0]) ....works
13th May 2020, 4:24 PM
pooja sharma
pooja sharma - avatar
0
Can u explain me with an example?
13th May 2020, 4:28 PM
pooja sharma
pooja sharma - avatar