Write a c program to find size of structure without using sizeof operator? This is very similar to how to find size of array without using sizeof operator. Learn the concept from http://wikistack.com/how-to-print-size-of-array-without-using-sizeof-operator.

find size of structure without using sizeof operator sample code

How it works:

  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 struct foo arr[2];
  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.

Based on the above points another way to find the size of structure without using sizeof operator:-

ref:

https://stackoverflow.com/questions/7383047/c-find-the-size-of-structure




Related Contents to follow