免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1749 | 回复: 4
打印 上一主题 下一主题

[C] 这样的typedef的如何理解? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-03-28 18:52 |只看该作者 |倒序浏览
本帖最后由 testh 于 2013-03-28 18:53 编辑
  1. 29 struct snd_kcontrol;                                                                                                                          
  2. 30 typedef int (snd_kcontrol_info_t) (struct snd_kcontrol * kcontrol, struct snd_ctl_elem_info * uinfo);                                         
  3. 31 typedef int (snd_kcontrol_get_t) (struct snd_kcontrol * kcontrol, struct snd_ctl_elem_value * ucontrol);                                      
  4. 32 typedef int (snd_kcontrol_put_t) (struct snd_kcontrol * kcontrol, struct snd_ctl_elem_value * ucontrol);                                      
  5. 33 typedef int (snd_kcontrol_tlv_rw_t)(struct snd_kcontrol *kcontrol,                                                                           
  6. 34             int op_flag, /* 0=read,1=write,-1=command */                                                                                      
  7. 35             unsigned int size,                                                                                                               
  8. 36             unsigned int __user *tlv);                                                                                                        
  9. 37                                
复制代码
  1. 39 struct snd_kcontrol_new {                                                                                                                     
  2. 40   snd_ctl_elem_iface_t iface; /* interface identifier */                                                                                      
  3. 41   unsigned int device;    /* device/client number */                                                                                          
  4. 42   unsigned int subdevice;   /* subdevice (substream) number */                                                                                
  5. 43   unsigned char *name;    /* ASCII name of item */                                                                                            
  6. 44   unsigned int index;   /* index of item */                                                                                                   
  7. 45   unsigned int access;    /* access rights */                                                                                                
  8. 46   unsigned int count;   /* count of same elements */                                                                                          
  9. 47   snd_kcontrol_info_t *info;                                                                                                                  
  10. 48   snd_kcontrol_get_t *get;                                                                                                                    
  11. 49   snd_kcontrol_put_t *put;                                                                                                                    
  12. 50   union {                                                                                                                                    
  13. 51     snd_kcontrol_tlv_rw_t *c;                                                                                                                 
  14. 52     const unsigned int *p;                                                                                                                    
  15. 53   } tlv;                                                                                                                                      
  16. 54   unsigned long private_value;                                                                                                               
  17. 55 };
复制代码
常用的 typedef char s8 这样的简单用法了解,但是对于上面的
33 typedef int (snd_kcontrol_tlv_rw_t)(struct snd_kcontrol *kcontrol,                                                                           
34             int op_flag, /* 0=read,1=write,-1=command */                                                                                      
35             unsigned int size,                                                                                                               
36             unsigned int __user *tlv);   
该怎么理解,粗看,typedef 后面有3个参数,分别是 int ,(snd_kcontrol_tlv_rw_t),和(struct snd_kcontrol *kcontrol,int op_flag,unsigned int size,                                                                                                                unsigned int __user *tlv);
简写为typedef A B C,基本功不扎实了。。。。

论坛徽章:
0
2 [报告]
发表于 2013-03-28 21:37 |只看该作者
  1. typedef int (snd_kcontrol_tlv_rw_t)(struct snd_kcontrol *kcontrol,                                                                           
  2.                  int op_flag, /* 0=read,1=write,-1=command */                                                                                      
  3.                  unsigned int size,                                                                                                               
  4.                  unsigned int __user *tlv);   
复制代码
将一个返回值类型为int,参数为
  1. (struct snd_kcontrol *kcontrol,  int op_flag,  unsigned int size,  unsigned int __user *tlv)
复制代码
的函数类型重新定义为snd_kcontrol_tlv_rw_t

论坛徽章:
0
3 [报告]
发表于 2013-03-28 23:36 |只看该作者
可是这样的话,
     39 struct snd_kcontrol_new {                                                                                                                     
    40   snd_ctl_elem_iface_t iface; /* interface identifier */                                                                                      
    41   unsigned int device;    /* device/client number */                                                                                          
    42   unsigned int subdevice;   /* subdevice (substream) number */                                                                                
    43   unsigned char *name;    /* ASCII name of item */                                                                                            
    44   unsigned int index;   /* index of item */                                                                                                   
    45   unsigned int access;    /* access rights */                                                                                                
    46   unsigned int count;   /* count of same elements */                                                                                          
    47   snd_kcontrol_info_t *info;                                                                                                                  
    48   snd_kcontrol_get_t *get;                                                                                                                    
    49   snd_kcontrol_put_t *put;                                                                                                                    
    50   union {                                                                                                                                    
    51     snd_kcontrol_tlv_rw_t *c;                                                                                                                 
    52     const unsigned int *p;                                                                                                                    
    53   } tlv;                                                                                                                                      
    54   unsigned long private_value;                                                                                                               
    55 };

line50-line53 中定义一个 snd_kcontrol_tlv_rw_t 类型的指针 c ? 另外,在其他地方进行初始化的时候会给union tlv.p一个数组的地址,这样就把一个数组地址和一个指向函数的指针弄成一个union了,可以这么用吗? 这也是我理解成函数类型的疑问?
以上代码是 include/sound/control.h 中。

论坛徽章:
0
4 [报告]
发表于 2013-03-29 08:36 |只看该作者
有空一起交流一下

论坛徽章:
3
CU大牛徽章
日期:2013-03-13 15:15:08CU大牛徽章
日期:2013-03-13 15:26:06CU大牛徽章
日期:2013-03-13 15:26:47
5 [报告]
发表于 2013-03-29 09:40 |只看该作者
testh 发表于 2013-03-28 23:36
可是这样的话,
     39 struct snd_kcontrol_new {                                                    ...

snd_kcontrol_tlv_rw_t *c; 是定义了一个函数指针。

比如外部定义了一个函数 int fun(参数列表与前面对应的typedef语句里的一致),然后可以将该函数名赋值给函数指针c。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP