How to use static cast to allow the compiler to check the casting in C++

1 Answer

0 votes
#include <iostream>

int main() {
    char ch = 97;
    
    int *p = static_cast<int*>(&ch);
}




/*
run:

test.cpp: In function ‘int main()’:
test.cpp:6:14: error: invalid ‘static_cast’ from type ‘char*’ to type ‘int*’
    6 |     int *p = static_cast<int*>(&ch);

*/

 



answered Dec 4, 2022 by avibootz

Related questions

3 answers 204 views
204 views asked Feb 23, 2018 by avibootz
1 answer 96 views
96 views asked Feb 21, 2022 by avibootz
1 answer 170 views
1 answer 197 views
197 views asked Apr 22, 2022 by avibootz
...