简体   繁体   中英

How could I cast c++ const float* to char * type?

This might be some duplicated question, but I have not find an answer to it, so I bother to ask it again. Suppose I have a function which return the data with an const float* type, how could I convert it to char* for fstream.write to use?

const float *ptr = nullptr;
some_func(ptr);
// my expected operator
fstream fout(...);
fout.write(ptr, 512);

How could I convert the const float* pointer to a normal pointer please?

For your use case, the preferred method is to use reinterpret_cast .

fstream fout(...);
fout.write(reinterpret_cast<char const*>(ptr), 512);

PS

If ptr points to an array of 512 number of float s, you'll have to use:

fout.write(reinterpret_cast<char const*>(ptr), sizeof(float)*512);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM