I wanted to make an array of strings and sort them in order based on their length (smallest -> biggest) but the program crashes after all inputs.
And also it bypasses Element 0 (starts directly from element 1 during input)
#include <stdio.h>
#include <stdlib.h>
main()
{
int i,j,N;
printf("\nInput amount of alphanumericals: ");
scanf("%d",&N);
{
int min;
char *swap=(char*)malloc(sizeof(char)*150);
char *A[N],**temp;
for(i=0;i<N;i++)
*(A+i)=malloc(sizeof(char)*N);//Error Here
temp=A;
for(i=0;i<N;i++){
printf("\nInput %d element:",i+1);
fgets(temp+i,150,stdin);//And Here
}
printf("\n\nData [");
for(i=0;i<N;i++)
printf(" %s",A[i]);
printf(" ]\n\n");
//insertion sort
for(i=0;i<N;i++){
min=i;
for(j=i+1;j<N;j++){
if(strcmp(A[j],A[min])<0){
min=j;
}
}
if(min!=i){
swap=A[i];
A[i]=A[min];
A[min]=swap;
}
}
free(swap);
printf("\n\nInsertion Sorted Data [");
for(i=0;i<N;i++)
printf(" %s",A[i]);
printf(" ]");
}
return 0;
}