I know C++ and know the function sizeof itself but I need to write my own sizeof function so please explain how it works exactly? What does it do with the parameter
sizeof isn't a function. It's an operator in C.
We can implement its functionality something like follows.
#include
#define mysizeof(X) \
({ \
__typeof(X) x; \
(char *) (&x+1) - (char*) (&x); \
})
int main()
{
struct sample
{
int a;float b; char c;
};
printf("%d", mysizeof(struct sample));
return 0;
}
Answer : 12