sort and swap functions take pointer as argument, when I try it with 2 or 3 strings it works fine but for more than three its giving a segmentation fault, this is my code please let me know what's going on here and why it's giving this error.
#include <stdio.h>
#include <string.h>
void get(int r, int c, char (*s)[c]);
void print(int r, int c, char (*s)[c]);
void sort(int r, int c, char (*s)[c]);
void swap(int c, char (*s)[c], char (*s1)[c]);
void main()
{
int r;
printf("\n\t enter no. : ");
scanf("%d", &r);
char s[r][31];
get(r,31,s);
sort(r,31,s);
printf("\n\tsorted list");
print(r,31,s);
}
void get(int r, int c, char (*s)[c])
{
int i, j;
for(i = 0; i < r; i++)
{
printf("\n\t");
scanf("%s", *(s+i));
}
}
void print(int r, int c, char (*s)[c])
{
int i, j;
for(i = 0; i < r; i++)
{
printf("\n\t%s", *(s+i));
}
}
void sort(int r, int c, char (*s)[c])
{
int i, j, k;
for(i = 0; i < r; i++)
{
for(j = 0, k = 1; j < r-1; j++, k++)
{
if(strcmp(*(s+j),*(s+k)) > 0)
{
swap(c, (s+j), (s+k));
}
}
}
}
void swap(int c, char (*s)[c], char (*s1)[c])
{
char (*t)[c];
strcpy(*t, *s);
strcpy(*s,*s1);
strcpy(*s1,*t);
}