Home:ALL Converter>Typecasting the Reference of a Double Variable

Typecasting the Reference of a Double Variable

Ask Time:2020-12-30T06:07:50         Author:The Programming M16A4

Json Formatter

I have a question about typecasting the reference of a variable of type double. What is exactly happening when &d becomes typecasted into an unsigned char*? How is typecasting the address of a variable valid and what is it actually doing?

#include <stdio.h>
 
int main(void) {
    // examining object representation is a legitimate use of cast
    double d = 3.14;
    printf("The double %.2f(%a) is: ", d, d);
    for(size_t n = 0; n < sizeof d; ++n)
        printf("0x%02x ", ((unsigned char*)&d)[n]); // <--
}

And how do the array brackets work in this case?

Author:The Programming M16A4,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/65499406/typecasting-the-reference-of-a-double-variable
yy