简体   繁体   English

我如何将 c++ const float* 转换为 char * 类型?

[英]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*类型返回数据,我如何将其转换为char*以供fstream.write使用?

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?请问如何将const float*指针转换为普通指针?

For your use case, the preferred method is to use reinterpret_cast .对于您的用例,首选方法是使用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:如果ptr指向一个包含512float的数组,则必须使用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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