Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to cast address of int to char pointer

If I need to read int from ifstream

int myInt = 0;
fileStream.read(reinterpret_cast<char*>(&myInt), sizeof(int));

is using reinterpret_cast<char*> correct way to accomplish that?

like image 513
dextrey Avatar asked Dec 20 '11 13:12

dextrey


1 Answers

is using reinterpret_cast correct way to accomplish that?

Yes. Prefer c++ style casts, instead of c style casts.

As suggested in comments, a better way to use the read method function is :

int myInt = 0;
fileStream.read(reinterpret_cast<char*>(&myInt), sizeof(myInt));
like image 118
BЈовић Avatar answered Oct 03 '22 17:10

BЈовић