My code takes in a series of words as command line arguments and should sort them using qsort. Right now I print out the original Args however when it goes to print the Sorted args I receive a segmentation error. New to C, all advice is appreciated.
#include <stdlib.h>
#include <string.h>
int stringCmp(const void *str1, const void *str2); //Function prototype.
int main (int argc, char *argv[])
{
int i;
char **arr = malloc(argc * sizeof(char *));
printf("Original Args:\n");
for (i = 0; i < argc-1; i++){
arr[i] = argv[i+1];
printf("%s\n",arr[i]);
}
qsort(arr, argc, sizeof *arr, stringCmp);
printf("\nSorted Args:\n");
for (i = 0; i < argc; i++){
printf("%s\n", arr[i]);
}
free (arr);
return 0;
}
int stringCmp(const void *str1, const void *str2)
{
strcmp(str1, str2);
}