Possible Duplicate:
How to find the size of a variable with out using sizeof
using C, can we somehow find the size of a pointer without using sizeof operator ? though i found this question at lot of places while googling, but i couldn’t find any answer. any help will be greatly appreciated. thanks
Here is a small but legal hack that you can use:
Here is how it works:
Nto a pointer addsNtimes the size of the type pointed to; hence(ptr+1)addssizeof(int*)to the “raw” value ofptrsizeof(char)is always1, so casting tochar*lets you subtract “raw” values; the result is thesizeofadded by the(ptr+1)operation.