how to use built in sorting function alphasort

can any one help me with this function through an example..

#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
   struct dirent **namelist;
   int i,n;

   n = scandir(".", &namelist, 0, alphasort);
   if (n < 0)
   {
        perror("scandir");
        exit(1);
   }
  
   for (i = 0; i < n; i++) 
   {
      printf("%s\n", namelist->d_name);
      free(namelist);
   }
   free(namelist);
   
   return 0;
}

Where the directory name "." is the current directory.