[英]How do I copy a const float* const to float*?
So in my function, I was passed a parameter const float* const d_inputArray
How do I read its content and copy to a different array float* d_tempInput
?所以在我的函数中,我被传递了一个参数
const float* const d_inputArray
如何读取它的内容并复制到不同的数组float* d_tempInput
? I don't know how do deal with either const.我不知道如何处理任何一个 const。
假设两个指针都指向相同大小的缓冲区:
memcpy(d_tempInput, d_inputArray, LENGTH_OF_BUFFER_IN_BYTES);
/* malloc memory for d_tempInput */
d_tempInput = (float *) malloc(sizeof(d_inputArray));
/* memcpy everything from d_inputArray into d_tempInput */
memcpy(d_tempInput, d_inputArray, sizeof(d_inputArray));
/* Free when done */
free(d_tempInput);
The keyword const
refers to the content of the memory region the pointer points to.关键字
const
指的是指针指向的内存区域的内容。
Do not confuse this with a constant pointer (construction by float * const pntr
).不要将其与常量指针(由
float * const pntr
) float * const pntr
。 Read Constant Pointers and Pointers to Constants for more info.阅读常量指针和指向常量的指针以获取更多信息。
Code代码
Interactive version: here互动版:这里
#include <iostream>
#include <cstring>
int main()
{
size_t N = 3;
const float a[N] = {100, 101, 102};
//float *b = a; // not possible
float *b = new float[N];
std::memcpy(b, a, N*sizeof(float));
const float *c = b;
for (size_t ii = 0; ii < N; ++ii)
{
std::cout << a[ii] << "; " << b[ii] << "; " << c[ii] << std::endl;
}
delete b;
return 0;
}
Output输出
100; 100; 100
101; 101; 101
102; 102; 102
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.