title: How to shift element of array to right on one position ? C language
body: <p>I have to write function (array_rshift) to shift every element of array right by 1 position using only pointers on beggining and end of array. I have code to shift elements by left, but I really don't understand how to do it for right side.</p>
  
  <p>Here some code:</p>
  
  <pre><code>
  #include "pch.h"
  #include &lt;stdio.h&gt;
  
  int arr[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  int *begin = arr;
  int *end = arr + 10;
  int copy[10] = { 0, };
  
  #define PRINT(a) \
  for (size_t i = 0; i &lt; size; i++)\
      if (i != size - 1)\
          printf("%2i, ", *(a + i));\
      else\
          printf("%2i]\n", *(a + i))\
  
  // Swap the contents of two variables.
  void swap(int* a, int* b)
  {
      int tmp = *a;
      *a = *b;
      *b = tmp;
  }
  
  // Return the sum of the array from 'begin' to 'end'.
  int array_sum(int *begin, int *end)
  {
      int size = end - begin;
      int p = 0;
      int result = 0;
      while (size)
      {
  
          result += *(begin + p);
          p++;
          size -= 1;
      }
      return result;
  
  }
  
  // Reverse the array from 'begin' to 'end'.
  void array_reverse(int *begin, int *end)
  {
  
      end--;
      while (begin &lt; end)
      {
  
          swap(begin, end);
  
          begin++;
          end--;
  
      }
  }
  
  // Copy the array from 'begin' to 'end' into 'dst'.
  // The dst array must be large enough.
  // The two arrays do not overlap.
  void array_copy(int *dst, int *begin, int *end)
  {
      while (begin &lt; end)
      {
          *dst = *begin;
          begin++;
          dst++;
      }
  
  }
  
  // Shift the array from 'begin' to 'end' by one position to the right.
  // The leftmost value will not change.
  // The rightmost value will be lost.
  // For instance:
  // - Before: 1 2 3 4 5
  // - After:  1 1 2 3 4
  void array_rshift(int *begin, int *end)
  {   
      int size = end - begin;
      for (int i = 0; i &lt; size -1; i++)
      {
          *(begin + i) = *(begin + i + 1);
      }
  }
  
  int main()
  
  {
      size_t size = end - begin;
  
      printf("Initial arr  = [");
      PRINT(arr);
  
      int sum = array_sum(begin, end);
      printf("sum          = %2i\n", sum);
  
      array_reverse(begin, end);
      printf("Reversed arr = [");
      PRINT(arr);
  
      array_copy(copy, begin, end);
      printf("Copy         = [");
      PRINT(copy);
  
      array_rshift(begin, end);
      array_rshift(begin, end);
      array_rshift(begin, end);
      printf("Shifted arr  = [");
      PRINT(arr);
  }
  </code></pre>
  
  <p>Expected result as follows:</p>
  
  <pre><code>Shifted arr  = [ 10, 10, 10, 10, 9, 8, 7, 6, 5, 4]
  </code></pre>
  
  <p>But I have this:</p>
  
  <pre><code>Shifted arr  = [ 7, 6, 5, 4, 3, 2, 1, 1, 1, 1]
  </code></pre>
  
  <p>Someone know how to do this ?</p>