shifting arrays

This is a discussion on shifting arrays within the C Programming forums, part of the General Programming Boards category; is there a library function which does something similar to shifting each of the elements in an array up or ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    26

    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.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >You will of course want to copy to a temporary buffer and then back
    Or you could just use memmove.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    26
    aha yes, memmove is what i wanted, thanks.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yeah, and you could have just as easily wrote a for loop to do it.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    26
    i know, but since someone's already written one.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  4. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM
  5. sorting arrays
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-13-2001, 06:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21