I have a fixed size array and i want to 'left shift' it.
e.g {1,2,3,4} --> {2,3,4}
from my understanding arrays are consecutive memory cells, so this should be possible by just changing the start pointer.
is this possible in C?
I tried
array = &array[1];
but I get
error: assignment to expression with array type
I can do
char* array1;
array1 = &array2[1];
and use array1, but this introduces another variable and id like, if possible, to use less memory
Arrays are non-modifiable lvalues. So this statement
is incorrect.
This introducing a pointer
does not shift to the left elements of the array. They stay in their original positions. There is only declared a pointer that points to the second element of the array.
To shift to the left elements in an array you need to copy following elements to preceding elements. If to shift only to one position you can use standard C string function
memmoveor to use a loop. For examplePay attention to that the actual size of the array will not be changed. Arrays are extents of memory of fixed sizes.