Code- Runtime Error(SIGSEGV)

#include<stdio.h>

#include<string.h>

int main(){

int t,n,k,ans,i,sum;

int *a;
char *op=(char*)malloc(4);
scanf("%d",&t);

while(t-- >0){
  scanf("%d %d %d",&n,&k,&ans);
  a=(int*)malloc(sizeof(int)*n);
  for(i=0;i<n;i++){
       scanf("%d",a+i);
  }
  scanf("%s",op);
  if(strcmp(op,"XOR")==0){
  
    sum=a[0];
    for(i=1;i<n;i++)
       sum=sum^a[i];
    if(k%2!=0)
     printf("%d\n",ans^sum);
    else
     printf("%d\n",ans^0);
  }
  else if(strcmp(op,"AND")==0){
    sum=a[0];
    for(i=1;i<n;i++)
      sum=sum & a[i];
    printf("%d\n",ans & sum);  
    
  }
  else{
    sum=a[0];
    for(i=1;i<n;i++)
      sum=sum | a[i];
    printf("%d\n",ans | sum);  
  }
  
}

//getch();
return 0;

}
//here is the link to the problem :http://www.codechef.com/problems/RRCODE/

Give size to the character pointer. You are not giving specified memory to the pointer which will write input in some non-authorized location which may not be accessed when you wanted to.

Add this statement after declaring character pointer…

op = malloc(5);

i have made the new changes and this time it is showing wrong answer

Check for K = 0 cases. (According to the given function, if K = 0, you should not do AND/OR operations for complete array).

Thankx.MY code run perfectly.I miised out the constraints that k>=0;its now perfectly running