separate-array-of-pointers technique

what is separate-array-of-pointers technique? when and where can it be used?
here I will explain it.
separate-array-of-pointers is a array of pointers each of which points at one element of another array.when we sort a array  whose elements may be a large structure,it may be useful. As the array is sorted ,it will spend much time. Beause a lot of data will be copied back and forth.we can exploite separate-array-of-pointers technique.

/*
* demonstrate the use of separate-array-of-pointers technique.
*  
*/

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

struct employee
{
       
char name[20];
       
long emp_id;
       }
;
int  emp_pointer_name_compare( const   void   * e1p, const   void   * e2p)
{
  
const struct employee **e1,**e2;
  e1
=(const struct employee* *) (e1p); //it is hard to understand?
  e2=(const struct employee* *) (e2p);
  
return strcmp((*e1)->name,(*e2)->name);
     
      
}

/*
int emp_name_compare(const void *e1p,const void * e2p)
{
  const struct employee *e1,*e2;
  e1=(const struct employee* ) (e1p); //it is hard to understand?
  e2=(const struct employee* ) (e2p);
  return strcmp(e1->name,e2->name);
}
*/

int  main()
{
struct employee employees[
10];
struct employee 
*pemployees[10];
char buf[BUFSIZ];
int i,nep;
for(nep=0;nep<3 &&fgets(buf,BUFSIZ,stdin)!=NULL;nep++)
{
             sscanf(buf,
"%s %ld/n",&employees[nep].name,
                                   
&employees[nep].emp_id);
             pemployees[nep]
=&employees[nep];                     
                                   
}
   
qsort(pemployees,nep,sizeof(struct employee 
*),emp_pointer_name_compare);

/*qsort(employees,nep,sizeof(struct employee),emp__name_compare);*/

printf(
"Sorted by name:");
for(i=0;i<nep;i++)
{
 printf(
"%s %ld /n",pemployees[i]->name,pemployees[i]->emp_id);
}
 
scanf(
"%d",&i);
return 0;    
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值