Sorting an array of strings using pointers (followup) This is a follow-up to this post.

Things I've changed:


The number of strings is checked.
I'm using pointers now.


I'll use fgets in more mission critical situations. For a lab assignment, the scanf approach will suffice I hope.

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#define LEN 100
#define WID 80

void sort(char *s[], int n);

void display(char *s[], int n);

int main()
{
    int n, i;
    char *s[LEN];
    printf("Enter number of strings : ");
    scanf("%d", &amp;n);
    printf("Enter strings one by one :\n");
    if (n &gt; LEN) {
        printf("Sorry, maximum strings allowed is %d. Defaulting.", LEN);
        n = LEN;
    }
    for (i = 0; i &lt; n; i++) {
        printf("%d : ", i + 1);
        s[i] = (char *) malloc(WID * sizeof(char));
        scanf(" %s", s[i]);
    }

    printf("The initial elements are:\n");
    display(s, n);
    sort(s, n);
    printf("The elements after sorting are:\n");
    display(s, n);

    return 0;
}

void sort(char *s[], int n)
{
    char *temp;
    int item, i;

    for (item = 1; item &lt; n; item++) {
        temp = s[item];
        for (i = item; i &gt; 0 &amp;&amp; strcmpi(s[i - 1], temp) &gt; 0; i--);
        memcpy(&amp;s[i + 1], &amp;s[i], (item - i) * sizeof(char *));
        s[i] = temp;
    }
}

void display(char *s[], int n)
{
    int i;
    printf("\n\n");
    for (i = 0; i &lt; n; i++) {
        printf("%s ", s[i]);
    }
    printf("\n\n");
}


Any alternative or more elegant approach is welcome.
Advice 1

I suggest that you add the { and } to loop/conditional block even in the case it's a one-liner. The reason is if someone maintains your code and need to add a statement or a couple, they do not need to type those.

Advice 2

The display may print a more friendly text:

void display(char *s[], int n)
{
    int i;
    printf("\n\n[");
    char* separator = "";
    for (i = 0; i &lt; n; i++) {
        printf("%s%s", separator, s[i]);
        separator = ", ";
    }
    printf("]\n\n");
}


For example: [abc, bb, def].

Advice 3

If you need to sort large array of strings, there are more efficient algorithms especially for sorting strings. See this. Also, I believe radix sort would perform good as well.

Hope that helps.