Using qsort to sort an array of strings
I understand that qsort will pass my function two char**'s and use it to sort my array of strings. Can someone help me with comparing two strings that were passed to my function as char**'s?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int sortstring( const void **str1, const void **str2 );
typedef int (*funcptr)();
int main (int argc, const char * argv[])
{
int inx = 0;
funcptr p_sortstring;
char *starr[] =
{
"dog",
"Arm",
"Cat",
"Eric",
"Bat",
"Fork",
"hello",
"Geroge",
"Mike",
"Alabama"
};
size_t count = sizeof(starr)/sizeof(*starr);
qsort( starr, count, sizeof(char), p_sortstring );
for (inx = 0; inx < 10; inx++)
printf("%s ", starr[inx]);
return 0;
}
static int sortstring( const void **str1, const void **str2 )
{
int result = 0;
const char *rec1 = *str1;
const char *rec2 = *str2;
int val = strcmp(rec1, rec2);
if ( val < 0 )
{
result = -1;
}
else if ( val > 0 )
{
result = 1;
}
else
{
result = 0;
}
return result;
}