我尝试将 psamples2 数组指针传递给函数,但它不断传递垃圾(结束数组?)
另外为什么 cpointer 保留迭代值而不是数组的开头?
我的问题是为什么 psamples2 没有指向数组的开头。
# define kiss_fft_scalar float
float samples[2048] = {0};
float* psamples = samples;
float* psamples2 = samples;
int offset = 0;
while (!_exitThread) { // some where in the code its false
short* target = psamples + offset;;
if(offset<2048)
{
float *src = offset; //just numbers
*target = *src;
// Now the psamples and the psamples2 pointers are updated with pointing to the element
//in the array and not to the beginning why ?
}
_exitThread = true;
}
kiss_fftr( (kiss_fft_scalar*)psamples );
// ---------------------- This is function in some module in the app here is just the signature -----
// this is the C function that getting the kiss_fft_scalar *timedata that revived as garbage
void kiss_fftr( const kiss_fft_scalar *timedata )
{
// the result of *timedata is 8.52120011e-038 as i understand is should be the pointer to the array
}
最佳答案
首先是循环体
while(!_exitThread)
设置了_exitThread
,那么简单的if()
语句就足够了吗?
其次,您将指针类型从 float*
切换为 short*
并通过添加 offset
来混淆情况,并且不清楚编译器是否将添加 offset * sizeof(float)
或 offset * sizeof(short)
。
short* target = psamples + offset;;
第三,尽管您提示它,但您没有显示您在哪里使用psamples2
。第四,在使用 int
值设置它指向的 float
之前,您还没有初始化 src
;
float *src = offset;
您正在将一个int
分配给一个指针。为什么不打开编译器警告?
关于c - 为什么复制后数组指针不指向数组的开头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27378878/