mercredi 12 août 2020

how does reinterpret_cast works in the following case

I am a new to C++ style coding and i am confused with reinterpret_cast and regular c style cast.

code 1:

int a=100;
    char *p =reinterpret_cast<char*>(&a);
    cout << *p;

gives me out put "d" and i understood it as asci value but the below code

code 2:

 char c=10;
    int *p = reinterpret_cast<int *>(&c);
    cout << *p;

is giving me some random output.

i thought there will be data loss in code 1 since int is converted to char but it seems like this is happening in code 2. Please help me in understanding this. Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire