c - 为什么复制后数组指针不指向数组的开头

标签 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 
}

最佳答案

首先是循环体

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/

相关文章:

JavaScript 数组 : problems with array declaration and access

c - 局部变量的地址是否总是保证有效

c++ - 为什么 cout 打印 char 数组与其他数组不同?

c - 如何清除c中的字符指针数组?

C - "Connection refused"在一个已经打开的套接字上

C 宏 : how do I concatenate a name and a number which is result of a math operation (done also by preprocessor)?

C 传递为指针

c - 使用字符指针数组存储多个char数组

C语言: The Array did not return a correct value

c++ - 将 "vector of vector"复制到“array of array”