title: Hackerrank circular array rotation segmentation faults
body: <p>My following code sucessfully runs in sample input but gives segmentation faults in  13 test cases.</p>
  
  <pre><code>#include &lt;math.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;assert.h&gt;
  #include &lt;limits.h&gt;
  #include &lt;stdbool.h&gt;
  
  int main(){
      int n; 
      int k; 
      int q; 
      int index[q];
      scanf("%d %d %d",&amp;n,&amp;k,&amp;q);
      int *a = (malloc(sizeof(int) * n));
      for(int a_i = 0; a_i &lt; n; a_i++){
         scanf("%d",&amp;a[a_i]);
      }
      for(int a0 = 0; a0 &lt; q; a0++){
          int m; 
          scanf("%d",&amp;m);
          index[a0] = m;
      }
      for(int i=0; i&lt;k; i++){
          int ap = a[n-2];       
  
          for(int p=1; p&lt;n-1; p++){
              a[p] = a[p-1];
          }
          a[0] = a[n-1];
          a[n-1] = ap;      
  
  
      }
      for(int j=0; j&lt;q;j++){
          printf("%d\n", a[index[j]]);
      }
  
      return 0;
  }
  </code></pre>
  
  <p>I am unable to find where the segmentation fault is. Also check out this:<a href="https://stackoverflow.com/questions/41512872/can-malloc-be-used-to-define-the-size-of-an-array/41513426#41513426">where I asked about declaring a as a pointer using malloc</a></p>
  
  <p>There might have been chances that using malloc() to declare a would have lead to segmentation faults since it does not check for allocation error, but even when i defined a as an array the problem still remained.</p>