C:访问动态数组

时间:2018-10-10 22:04:10

标签: c arrays vector dynamic

我正在尝试在动态分配的数组中打印一个变量,但是它返回了错误的值。

这是数组声明: int16_t *sample_vector = malloc (num_samples*sizeof(int16_t));

打印尝试

sample_vector[i] =  data_buffer[0] | 
               (data_buffer[1]<<8) | 
               (data_buffer[2]<<16)| 
               (data_buffer[3]<<24);

printf ("Sample vector [%ld]: %d\n", i, sample_vector [i]); //this works fine, it prints shows the real sample value

printf ("Sample vector [%ld]: %d\n", (i-1), sample_vector [i-1]); //this doesn't work, it prints garbage

0 个答案:

没有答案