title: mergesort array of int using pointers
body: <p>For school I have to program a merge sort using only pointers. 
  I've tried almost everything, but I can not get it working.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  #define num_elementi(array) (sizeof(array)/sizeof(array[0]))
  
  void selsort(int arr[],int n);
  void swap(int * a, int * b);
  void print(int arr[],int n);
  void insort(int arr[],int n);
  void mergesort(int arr[],int *p,int *u);
  void merge(int arr[],int * p, int * q,int * u);
  
  int main(int argc, char *argv[]){
      int arr[]={99,12,14,65,2,7,54,78,5,1,43,59,88,28,61};
      int n=num_elementi(arr);
      printf("numero elementi array: %d\n",n);
      print(arr,n);
      printf("numero elementi array: %d\n",n);    
      mergesort(arr,&amp;arr[0],&amp;arr[n-1]);
      print(arr,n);
      system("pause");
  }
  
  void selsort(int arr[],int n){
      int *i,*j;
      for(i=arr;i&lt;&amp;arr[n-1];i++){
          for(j=i+1;j&lt;&amp;arr[n];j++){
              if(*j&lt;*i)swap(i,j);;
          }
  
      }
  }
  
  void swap(int * a, int * b){
      int temp;
      temp=*a;
      *a=*b;
      *b=temp;
  }
  
  void print(int arr[],int n){
      int * p=arr;
      for(p;p&lt;&amp;arr[n];p++)printf("%d ",*p);
      printf("\n");
  }
  
  void insort(int arr[],int n){
      int *i,*k;
      for(i=&amp;arr[1];i&lt;&amp;arr[n];i++){
          k=i-1;
          while(k&gt;=&amp;arr[0]&amp;&amp; *k&gt;*(k+1)){
              swap(k,k+1);
              k=k-1;
          }
      }
  }
  
  
  void mergesort(int arr[],int *p,int *u){
      if (p&lt;u){
          int *q=((u-p)/2)+p;
          mergesort(arr,p,q);
          mergesort(arr,q,u-1);
          merge(arr,p,q,u);
      }
  
  }
  
  void merge(int arr[],int * p, int * q,int * u){
      int arr1[u-p]; //inizializzazione array di dimensioni utili
      int * i=p; //puntatore al primo elemento di arr
      int *j=q+1; //puntatore al elemento di mezzo +1 di arr
      int *k= arr1; //puntatore al primo elemento di arr1
      if (u-p==1){ 
          if (*u&lt;*p){
              swap(u,p);
          }
          return;
      }
      while(i&lt;q &amp;&amp; j&lt;u){
          if(*i&lt;*j){ 
              *k=*i;
              i=i+1;
          }
          else{
              *k=*j;
              j=j+1;
          }
          k=k+1;
      }
      while(i&lt;q){*k=*i;i++;k++;}
      while(j&lt;u){*k=*j;j++;k++;}
  
      i=p;
      k=arr1;
      for(i,k;i&lt;&amp;arr[u-p];i++,k++){
          *i=*k;
      }
  }
  </code></pre>
  
  <p>Can someone please explain what I did wrong? Thank you so much!!</p>
  
  <p>PS: Please forgive my really bad English.</p>
  
  <p><strong><em>EDIT</em></strong> new code for the suggestion of Maciej Hehl</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  #define num_elementi(array) (sizeof(array)/sizeof(array[0]))
  
  void swap(int * a, int * b);
  void print(int arr[],int n);
  void mergesort(int arr[],int * arr_begin,int * arr_end);
  void merge(int * destination, int * r1_begin, int * r1_end, int * r2_begin, int * r2_end);
  
  int main(int argc, char *argv[]){
      int arr[]={99,12,14,65,2,7,54,78,5,1,43,59,88,28,61};
      int n=num_elementi(arr);
      print(arr,n);  
      mergesort(arr,&amp;arr[0],&amp;arr[n-1]);
      print(arr,n);
      system("pause");
  }
  
  void swap(int * a, int * b){
      int temp;
      temp=*a;
      *a=*b;
      *b=temp;
  }
  
  void print(int arr[],int n){
      int * p=arr;
      for(p;p&lt;&amp;arr[n];p++)printf("%d ",*p);
      printf("\n");
  }
  
  void mergesort(int arr[],int * arr_begin,int * arr_end){
      int * med,*arr1,*p,*p1;
      printf("i'm doing something\n");
      if(arr_begin&lt;arr_end){
          med=arr[arr_end-arr_begin]/2+arr_begin;
          mergesort(arr,arr_begin,med);
          mergesort(arr,med+1,arr_end);
          arr1=malloc((arr_end-arr_begin)*sizeof(int));
          printf("msort calls ended begin merge\n");
          merge(arr1,arr_begin,med,med+1,arr_end);
          for(p=arr,p1=arr1;p&lt;arr_end&amp;&amp;p1&lt;&amp;arr1[arr_end-arr_begin];p++,p1++){
              *p=*p1;
          }
      }
  
  }
  
  void merge(int * destination, int * r1_begin, int * r1_end, int * r2_begin, int * r2_end){
      int *pdest=destination;
      if (r1_end-r1_begin==1){ 
          if (*r1_end&lt;*r1_begin){
              swap(r1_end,r1_begin);
          }
          return;
      }
      if (r2_end-r2_begin==1){ 
          if (*r2_end&lt;*r2_begin){
              swap(r2_end,r2_begin);
          }
          return;
      }
      while(r1_begin&lt;=r1_end&amp;&amp;r2_begin&lt;=r2_end){
          if(*r1_begin&lt;*r2_begin){
              *pdest=*r1_begin;
              r1_begin++;
          }
          else{
              *pdest=*r2_begin;
              r2_begin++;
          }
          pdest++;
      }
      while(r1_begin&lt;r1_end){
          *pdest=*r1_begin;
          r1_begin++;pdest++;        
      }
      while(r2_begin&lt;r2_end){
          *pdest=*r2_begin;
          r2_begin++;pdest++;
      }
  
  }
  </code></pre>