C库函数使用集

1 qsort()函数使用
#include  < stdio.h >
#include 
< stdlib.h >
typedef 
struct  PLANT {
    
int  num;
    
char  name[ 13 ];
    
}Plant;

int  Mycompare( const   void   * lth, const   void   * rth) // 形参应该是const void*类型
{
    Plant
*  lpl = (Plant * )lth;
    Plant
*  rpl = (Plant * )rth;
    
return  lpl -> num - rpl -> num;
}
int  main( void )
{
    
int  i,n;
    Plant plant[
5 ] = {{ 3 , " wangtian " },{ 1 , " wangshuo " },{ 5 , " wangru " },{ 2 , " wangfang " },{ 4 , " wangxiao " }};
    
    
for (i = 0 ;i < 5 ;i ++ )
        printf(
" %d\t%s\n " ,plant[i].num,plant[i].name);

    qsort(plant,
5 , sizeof (Plant),Mycompare);

    printf(
" \n " );

    
for (i = 0 ;i < 5 ;i ++ ) // 输出排好序后的结构体数组
        printf( " %d\t%s\n " ,plant[i].num,plant[i].name);
    printf(
" %d\n " , sizeof (Plant));

fflush(stdin);
// 刷新缓冲区
   char  q;
  scanf(
" %c " , & q);
  
return   0 ;



你可能感兴趣的:(函数)