0
How to find the size of an array...using sizeof operator?
7 odpowiedzi
+ 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.
+ 2
int arr[10];
int size = sizeof(arr)/sizeof(arr[0]);
+ 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.
+ 1
pooja sharma you simply divide the amount of memory taken by whole array by an element inside
0
But how sizeof(arr[0]) ....works
0
Can u explain me with an example?