I think you have a problem with your swap function' or the way you call it.
anyway, this implementation works:
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define SIZE 3
int main()
{
int i, j; int n=SIZE; //n is a user input in the original program.
char temporary_string[32];
char* arr_of_strings[SIZE];
char* temp = NULL;
//input data
for (int l = 0; l < 3; l++)
{
scanf("%s", temporary_string);
arr_of_strings[l] = malloc(((strlen(temporary_string) + 1) *sizeof(char)));
strcpy(arr_of_strings[l], temporary_string);
temporary_string[0] = '';
}
//the actual sort
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < SIZE - i - 1; j++)
{
if (strcmp(arr_of_strings[j], arr_of_strings[j + 1]) > 0) //simplification
{
//swap
temp = arr_of_strings[j];
arr_of_strings[j] = arr_of_strings[j + 1];
arr_of_strings[j + 1] = temp;
}
}
}
printf("The sorted strings are:
");
for (int i = 0; i < n; i++) {
printf("%s
" , arr_of_strings[i]);
}
// free allocated memory
for (int i = 0; i < n; i++) {
free(arr_of_strings[i]);
}
}
I hope it helps, if you have quastions I would be happy to answer =)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…