Possible Duplicate:
size of a datatype without using sizeof
This is question asked in a C interview I wanted to know what is the correct logic for this
If you are not having a sizeof operator in C, how will you get to know the size of an int ?
Use an array[*]:
int a[2];
int sizeof_int = (char*)(a+1) - (char*)(a);
Actually, due to a note in the section on pointer arithmetic, you don't even need an array, because for the purposes of pointer arithmetic an object behaves like an array of size 1, and an off-the-end pointer is legal for an array:
int a;
int sizeof_int = (char*)((&a)+1) - (char*)(&a);
[*] By which I mean, "a solution to the puzzle posed is to use an array". Don't actually use an array, use sizeof
!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With