Say I have an integer array like such
#define MAX 5
int bar [MAX] = {0};
int foo [MAX] = {3,1,0,0,0};
Now I'd like to shift this array so that all null entries are to the left, i.e. bar = {0,0,0,3,1}
.
I thought I could do this by
- Finding the number of shifts I must perform
- Using
memmove()
to do the shift.
I solved 1. with the following loop
for (shift = MAX - 1; shift >= 0; --shift) {
if (foo[shift]) break;
}
But I do not know how to use memmove()
now to perform the shift, I attempted to do memmove(bar + shift, foo, MAX * sizeof(*foo)-1);
but with no success.
Is memmove()
the correct tool for the job? How can I circulate an integer array as described? What if this is happening inside a function where bar
is a passed as a pointer?