-
shifting arrays
is there a library function which does something similar to shifting each of the elements in an array up or down (clearing the first element) and inserting the user input into the new empty one?
for example
"abcdef" and function passes 'g', it becomes "bcdefg" or the other way becoming "gabcde"... (doesnt matter what happens to the null terminator)
I know its not hard to write one yourself but it would be easier if one already exists.
-
You mean something like memcpy? You will of course want to copy to a temporary buffer and then back, rather than having both arguments the same array.
Quzah.
-
>You will of course want to copy to a temporary buffer and then back
Or you could just use memmove. ;)
-
aha yes, memmove is what i wanted, thanks.
-
Yeah, and you could have just as easily wrote a for loop to do it.
Quzah.
-
i know, but since someone's already written one.
-
Code:
#include <stdio.h>
void pusharray( char array[], size_t size, char push )
{
int x;
for( x = 1; x < size; x++ )
{
array[x-1] = array[x];
}
array[x-2] = push;
}
int main( void )
{
char array[] = "abcdef";
pusharray( array, sizeof array, 'g' );
printf("%s\n", array );
return 0;
}
True, but some times it's worth the learning experience to write your own.
Quzah.