If there is no sizeof operator in C then does those pointer arithmetic work or not ?
#define find_size(x) (((char *)(&x+1))-((char *)(&x)))
int main(){
int i;
char c;
double d;
printf("%d\n",find_size(i));
printf("%d\n",find_size(c));
printf("%d",find_size(d));
return 0;
}
hii there i learned this trick back in my college days to find the size of a variable without the use of sizeof() operator .
It works in almost all cases .Although sizeof () is a operator i think in assembly code it comes down to some piece of more basic code .
don't ask how to find size() if we don't have # define
That i don't know
int i = 1337;
printf("i(%d) is located at %d, next location is %d\n", i, &i, &i+1);
printf("Number of memory locations: %d - %d = %d\n", &i+1, &i, &i+1 - &i);
printf("Number of bytes : %d - %d = %d\n", &i+1, &i, (long)(&i+1) - (long)&i);
/**
* Output
* i(1337) is located at 1834544, next location is 1834548
* Number of memory locations: 1834548 - 1834544 = 1
* Number of bytes : 1834548 - 1834544 = 4
*/