为什么数组指针在复制后没有指向数组的开头

时间:2014-12-09 12:23:12

标签: c arrays pointers

我尝试将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 
}

1 个答案:

答案 0 :(得分:1)

首先是你的循环体

while(!_exitThread)

设置_exitThread那么普通if()语句就足够了吗?

其次,您正在将指针类型从float*切换到short*并通过添加offset来混淆情况,并且不清楚编译器是否会添加offset * sizeof(float)或{{1 }}

offset * sizeof(short)

第三,虽然你在抱怨,但你没有显示你在short* target = psamples + offset;; 使用的位置。第四,在设置psamples2指向src之前,您尚未初始化float;

int

您正在为指针分配float *src = offset; 。为什么不打开编译器警告?