#include <stdafx.h>
#include <stdio.h>
#include <conio.h>
#include<stdlib.h>
#define My_Sizeof(type) ((char*)((type*)0 +1) - (char*)(type*)0)
void main()
{
char a='1';
int b=My_Sizeof(int);
printf("size is %d",b);
_getch();
}
// can anybody help me to understand wt the macro does to calculate sizeof char datatype ?
Break it down:
(char*)(type*)0is zero(type*)0 +1does pointer arithmetic using pointers of type(type *), so(type *)0 + 1will be a pointer of offset exactly0 + 1 * sizeof(type) = sizeof(type)bytesWhen the difference is taken as
(type *)the difference is1. When the difference is taken with both types(char *), the difference issizeof(T) - 0 = sizeof(T)