tags:

views:

49

answers:

3

I am trying to move the float array ptr 256 "units" from the start so (256 * 4 bytes) for floats. I am receiving a compile time error.

long new_capture_length = 4096;
long step_size = 256;
float data[new_capture_length];
data+=step_size;

error: invalid operands to binary + (have ‘float[(long unsigned int)(new_capture_length)]’ and ‘float *’)

How can I achieve this?

+4  A: 

You cannot "move" an array. You can move a pointer into an array, for example:

long new_capture_length = 4096;
long step_size = 256;
float data[new_capture_length];
float* p_data = &data[0];

p_data+=step_size; /* p_data now points 256 floats into data, i.e. to data[256]  */

But the location of data itself can never be altered, since it is not a pointer.

I gave a somewhat more detailed answer to a very similar question recently: http://stackoverflow.com/questions/3613516/c-pointer-question/3613542 (I don't like to call "what's wrong with this code" type questions exact duplicates even if they have the same underlying problem).

Tyler McHenry
A: 

You can use memmove() for moving data if that is what you want to do.

Anders K.
A: 

Arrays can be used as pointers in some cases, but not all. This is one where you can't. You can't "move" an array like you can a pointer. You have to either say float* ptr = data + step size (if all you want is a pointer) or use memmove to move the entire array.

bta