How to print size of array without using sizeof operator? This question can be asked in the interview for the purpose of knowing how deep your understanding is about pointer concept in c programming. 

Let us discuss how this output comes:

How to print size of array without using sizeof operator

 How to print size of array without using sizeof operator

  1. Array elements are stored in continuous memory location.
  2. A pointer after increment always points to the immediate next location.
  3. Let us see in above example , the array declaration and initialization is like int arr[] = { 1,5,6,7,8,10,3,12 }.
  4. The arr without subscript [] , is a pointer which points to the base address of the array.
  5. The &arr is also a pointer which points to the location where arr points. But there is difference , &arr decays as pointer to its first element but the type is different. The size of this different type pointer is entire array rather than just one element.
  6. Thus (&arr + 1) will point after the end of the array. De-referencing to *(&arr + 1) gives the address after the end of the last element.
  7.  In this way the statement *(&arr + 1) – arr will give the size of array, because (&arr + 1) points after the end of the array and arr is pointer to first element of the array.