C Programming :: Floating Point Problems - Discussion
6 / 6
#include
#define mysizeof(a) (&a+1) - &a
void main()
{
float d;
printf("%d\n", mysizeof(d) );
}
Note: assume sizeof float is 8 bytes
#define mysizeof(a) (&a+1) - &a
void main()
{
float d;
printf("%d\n", mysizeof(d) );
}
Note: assume sizeof float is 8 bytes
A8
B4
C1
Dcompiler error
Show Explanation
#define mysizeof(a) (&a+1) - &a
The difference gives the result that how many variables of type of d can be stored in that amount of memory which will obviously be 1 for the type d. The result of pointer subtraction is in elements and not in bytes. so, that the below expression evaluates to 1. instead of 8.
If you above statement put like below, the answer will be 8.
#define mysizeof(a) (char*)(&a+1) - (char*)&a
typecasting it int char* and taking the difference will tell us how many variables of type char can be stored in the given memory space (the difference). Since each char requires only 1 Byte of memory therefore (amount of memory)/1 will give the number of bytes between two successive memory locations of the type of variable passed on to the macro and hence, the amount of memory that the variable of type d requires i.e 8 bytes.
Asked In ::
Tricky ... little bit .. basically in below line &a (address of float variable d) getting subtracted from address of variable + 1 i.e incrementing it with 1 gives the address where the next variable of the type d can be stored, the answer is 1 instead of 8 (as given float size is 8 byte).
#define mysizeof(a) (&a+1) - &a
The difference gives the result that how many variables of type of d can be stored in that amount of memory which will obviously be 1 for the type d. The result of pointer subtraction is in elements and not in bytes. so, that the below expression evaluates to 1. instead of 8.
If you above statement put like below, the answer will be 8.
#define mysizeof(a) (char*)(&a+1) - (char*)&a
typecasting it int char* and taking the difference will tell us how many variables of type char can be stored in the given memory space (the difference). Since each char requires only 1 Byte of memory therefore (amount of memory)/1 will give the number of bytes between two successive memory locations of the type of variable passed on to the macro and hence, the amount of memory that the variable of type d requires i.e 8 bytes.
Read Full Answer
Report Error
Please Login First Click Here