dextrey
dextrey

Reputation: 855

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?

Upvotes: 10

Views: 5787

Answers (1)

BЈовић
BЈовић

Reputation: 64203

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));

Upvotes: 15

Related Questions