Code runs perfectly on my laptop, but gives Segmentstion Fault.Please help
/*http://www.spoj.com/problems/SBANK/ */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void so(char *a[32],long int first,long int last)
{
long int pivot,j,i;
char temp[32];
if(first<last)
{
pivot=first;
i=first;
j=last;
while(i<j)
{
while(strcmp(a[i],a[pivot])<=0&&i<last)
i++;
while(strcmp(a[j],a[pivot])>0)
j--;
if(i<j)
{
strcpy(temp,a[i]);
strcpy(a[i],a[j]);
strcpy(a[j],temp);
}
}
strcpy(temp,a[pivot]);
strcpy(a[pivot],a[j]);
strcpy(a[j],temp);
so(a,first,j-1);
so(a,j+1,last);
}
}
void repeat(char **a,long int n)
{
long int i=0,b;
for(;i<n;)
{
b=0;
do
{
b++;
if(b==1)
{
printf("%s ",a[i]);
}
i++;
}while(i<n&&strcmp(a[i-1],a[i])==0);
printf("%ld\n",b);
}
printf("\n");
}
int main()
{
int t;
long int i,n;
char **a,x;
scanf("%d",&t);
scanf("%c",&x);
while(t--)
{
scanf("%ld",&n);
scanf("%c",&x);
a=(char**)malloc(n*sizeof(char));
for(i=0;i<n;i++)
{
a[i]=(char*)malloc(32*sizeof(char));
scanf("%[^\n]%*c",a[i]);
}
printf("\n");
so(a,0,n-1);
repeat(a,n);
free(a);
for(i=0;i<n;i++)
free(a[i]);
}
return(0);
}