|
こんな感じかな。
/* ---------- Standard Header Include ---------- */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* ************************************************************
関数名【 comp() 】
比較処理
処理概要
qsort() 用の比較関数
引数
1) elm1 : 比較対象要素
2) elm2 : 比較要素
戻り値
比較結果
************************************************************ */
int comp( const void *elm1, const void *elm2 )
{
return strcmp( *(char**)elm1, *(char**)elm2 );
}
/* ************************************************************
関数名【 main() 】
メイン処理
処理概要
メイン処理
引数
なし
戻り値
処理結果
************************************************************ */
int main( void )
{
/* ***** 内部変数定義 ***** */
int i;
char *str[] =
{
"abcd",
"defg",
"aabc",
"adf",
"cvb",
};
puts( "ソート前" );
for( i=0; i<sizeof(str)/sizeof(str[0]); i++ )
{
/* ***** 表示 ***** */
printf( " %s\n", str[i] );
}
/* ***** ソート ***** */
qsort( str, sizeof(str)/sizeof(str[0]), sizeof(str[0]), comp );
puts( "ソート後" );
for( i=0; i<sizeof(str)/sizeof(str[0]); i++ )
{
/* ***** 表示 ***** */
printf( " %s\n", str[i] );
}
return 0;
} /* ***** main() ***** */
|