以linux-3.2内核代码为例,结构体里面的函数的用法:

例,在某驱动文件中,定义了一个平台设备驱动:

  1. static struct platform_driver s3c24xx_led_driver = {
  2. .probe = s3c24xx_led_probe,
  3. .remove = s3c24xx_led_remove,
  4. .driver = {
  5. .name = "s3c24xx_led",
  6. .owner = THIS_MODULE,
  7. },
  8. };
  1. struct platform_driver而言,probe平台设备注册时需要的函数,remove是平台设备移除时时需要的函数。
    对于不同的硬件,其注册和移除的时候,有各自不同的部分操作,比如硬件上拉下拉、特殊寄存器cfg配置等。
    这些不同的操作就体现在例子中的s3c24xx_led_probe, s3c24xx_led_remove中。
  1. static int s3c24xx_led_remove(struct platform_device *dev)
  2. {
  3. struct s3c24xx_gpio_led *led = pdev_to_gpio(dev);
  4.  
  5. led_classdev_unregister(&led->cdev);
  6. kfree(led);
  7.  
  8. return 0;
  9. }
  10.  
  11. static int s3c24xx_led_probe(struct platform_device *dev)
  12. {
  13. struct s3c24xx_led_platdata *pdata = dev->dev.platform_data;
  14. struct s3c24xx_gpio_led *led;
  15. int ret;
  16.  
  17. led = kzalloc(sizeof(struct s3c24xx_gpio_led), GFP_KERNEL);
  18. if (led == NULL) {
  19. dev_err(&dev->dev, "No memory for device\n");
  20. return -ENOMEM;
  21. }
  22.  
  23. platform_set_drvdata(dev, led);
  24.  
  25. led->cdev.brightness_set = s3c24xx_led_set;
  26. led->cdev.default_trigger = pdata->def_trigger;
  27. led->cdev.name = pdata->name;
  28. led->cdev.flags |= LED_CORE_SUSPENDRESUME;
  29.  
  30. led->pdata = pdata;
  31.  
  32. /* no point in having a pull-up if we are always driving */
  33.  
  34. if (pdata->flags & S3C24XX_LEDF_TRISTATE) {
  35. s3c2410_gpio_setpin(pdata->gpio, 0);
  36. s3c2410_gpio_cfgpin(pdata->gpio, S3C2410_GPIO_INPUT);
  37. } else {
  38. s3c2410_gpio_pullup(pdata->gpio, 0);
  39. s3c2410_gpio_setpin(pdata->gpio, 0);
  40. s3c2410_gpio_cfgpin(pdata->gpio, S3C2410_GPIO_OUTPUT);
  41. }
  42.  
  43. /* register our new led device */
  44.  
  45. ret = led_classdev_register(&dev->dev, &led->cdev);
  46. if (ret < 0) {
  47. dev_err(&dev->dev, "led_classdev_register failed\n");
  48. kfree(led);
  49. return ret;
  50. }
  51.  
  52. return 0;
  53. }

  

  1. 利用struct,我们实现了一种面向对象的思想,实例化的结构体对象中,有描述这个对象的行为方式(函数),有描述对象特征值或者对象组成的变量(变量,结构体变量等)。
  1.  

下面是设备结构体定义供参考:

  1. struct platform_driver {
  2. int (*probe)(struct platform_device *);
  3. int (*remove)(struct platform_device *);
  4. void (*shutdown)(struct platform_device *);
  5. int (*suspend)(struct platform_device *, pm_message_t state);
  6. int (*resume)(struct platform_device *);
  7. struct device_driver driver;
  8. const struct platform_device_id *id_table;
  9. };
  1. /**
  2. * struct device_driver - The basic device driver structure
  3. * @name: Name of the device driver.
  4. * @bus: The bus which the device of this driver belongs to.
  5. * @owner: The module owner.
  6. * @mod_name: Used for built-in modules.
  7. * @suppress_bind_attrs: Disables bind/unbind via sysfs.
  8. * @of_match_table: The open firmware table.
  9. * @probe: Called to query the existence of a specific device,
  10. * whether this driver can work with it, and bind the driver
  11. * to a specific device.
  12. * @remove: Called when the device is removed from the system to
  13. * unbind a device from this driver.
  14. * @shutdown: Called at shut-down time to quiesce the device.
  15. * @suspend: Called to put the device to sleep mode. Usually to a
  16. * low power state.
  17. * @resume: Called to bring a device from sleep mode.
  18. * @groups: Default attributes that get created by the driver core
  19. * automatically.
  20. * @pm: Power management operations of the device which matched
  21. * this driver.
  22. * @p: Driver core's private data, no one other than the driver
  23. * core can touch this.
  24. *
  25. * The device driver-model tracks all of the drivers known to the system.
  26. * The main reason for this tracking is to enable the driver core to match
  27. * up drivers with new devices. Once drivers are known objects within the
  28. * system, however, a number of other things become possible. Device drivers
  29. * can export information and configuration variables that are independent
  30. * of any specific device.
  31. */
  32. struct device_driver {
  33. const char *name;
  34. struct bus_type *bus;
  35.  
  36. struct module *owner;
  37. const char *mod_name; /* used for built-in modules */
  38.  
  39. bool suppress_bind_attrs; /* disables bind/unbind via sysfs */
  40.  
  41. const struct of_device_id *of_match_table;
  42.  
  43. int (*probe) (struct device *dev);
  44. int (*remove) (struct device *dev);
  45. void (*shutdown) (struct device *dev);
  46. int (*suspend) (struct device *dev, pm_message_t state);
  47. int (*resume) (struct device *dev);
  48. const struct attribute_group **groups;
  49.  
  50. const struct dev_pm_ops *pm;
  51.  
  52. struct driver_private *p;
  53. };

...

c语言,结构体里面的函数的更多相关文章

  1. C语言结构体中的函数指针

      这篇文章简单的叙述一下函数指针在结构体中的应用,为后面的一系列文章打下基础 本文地址:http://www.cnblogs.com/archimedes/p/function-pointer-in ...

  2. [编程] C语言结构体指针作为函数参数

    结构体指针作为函数参数:结构体变量名代表的是整个集合本身,作为函数参数时传递的整个集合,也就是所有成员,而不是像数组一样被编译器转换成一个指针.如果结构体成员较多,尤其是成员为数组时,传送的时间和空间 ...

  3. 在C语言结构体中添加成员函数

    我们在使用C语言的结构体时,经常都是只定义几个成员变量,而学过面向对象的人应该知道,我们定义类时,不只是定义了成员变量,还定义了成员方法,而类的结构和结构体非常的相似,所以,为什么不想想如何在C语言结 ...

  4. go语言结构体作为函数参数,采用的是值传递

    经过验证,go语言结构体作为函数参数,采用的是值传递.所以对于大型结构体传参,考虑到值传递的性能损耗,最好能采用指针传递. 验证代码: package main import ( "fmt& ...

  5. C语言结构体及函数传递数组參数演示样例

    注:makeSphere()函数返回Sphere结构体,main函数中.调用makeSphere()函数,传递的第一个參数为数组,传递的数组作为指针.

  6. C语言 结构体作为函数的参数

    1)使用结构体变量作为函数的参数 使用结构体变量作为函数的实参时,采用的是值传递,会将结构体变量所占内存单元的内容全部顺序传递给形参,形参必须是同类型的结构体变量 demo: # include &l ...

  7. c语言结构体

    [C语言]21-结构体 本文目录 一.什么是结构体 二.结构体的定义 三.结构体变量的定义 四.结构体的注意点 五.结构体的初始化 六.结构体的使用 七.结构体数组 八.结构体作为函数参数 九.指向结 ...

  8. 对嵌入式开发C语言结构体的一点总结

    今天冬至居然不上班,公司的良心啊!这回有心情写博客和日志了,好了,废话不多说.直接看下文: 鉴于嵌入式开发过程中,C语言结构体的使用当然是必不可少.话说,基础什么的比你会更牛逼的算法更重要,基础不牢, ...

  9. 将c语言的结构体定义变成对应的golang语言的结构体定义,并将golang语言结构体变量的指针传递给c语言,cast C struct to Go struct

    https://groups.google.com/forum/#!topic/golang-nuts/JkvR4dQy9t4 https://golang.org/misc/cgo/gmp/gmp. ...

随机推荐

  1. HTML5 事件

    下面的表格列出了可插入 HTML 5 元素中以定义事件行为的标准事件属性. Window 事件属性 - Window Event Attributes 表单事件 - Form Events 键盘事件 ...

  2. ASP.NET 定时通知

    ASP.NET 定时通知 using System; using System.Collections.Generic; using System.Linq; using System.Web; us ...

  3. nginx的 CPU参数worker_processes和worker_cpu_affinity使用说明

    官方说明: http://wiki.nginx.org/NginxChsHttpMainModule#worker_cpu_affinity http://wiki.nginx.org/NginxCh ...

  4. python爬虫实战1

    转载于:http://blog.csdn.net/dongnanyanhai/article/details/5552431 首先推荐一个网站:中医世家,这个网站上有很多关于中医的资料,光是提供的中医 ...

  5. IT第十天 - String和StringBuffer的比较、编程设计技巧整理、本周总结 ★★★

    IT第十天 上午 String 1.String在进行多次的+扩展时,会严重的降低处理效率,因为String长度是不可变的,在进行+运算改变字符串时,会自动创建很多临时字符串,并不是在原字符串上追加, ...

  6. for循环语句之求和,阶乘,求偶,求n次篮球蹦起高度

    for循环语句格式: ;;/*循环条件*/i++/*状态改变*/) { //循环体,执行代码:(break;跳出循环体) } for 穷举法用循环把各种可能的情况都走一遍,然后用if条件把满足要求的结 ...

  7. 内核必看: spinlock、 mutex 以及 semaphore

    linux 内核的几种锁介绍 http://wenku.baidu.com/link?url=RdvuOpN3RPiC5aY0fKi2Xqw2MyTnpZwZbE07JriN7raJ_L6Ss8Ru1 ...

  8. Android中EditText,Button等控件的设置

    EditText可以使用:TextView.setEnabled(true)来设置为可编辑,其实很简单,写在这里以便以后自己查看. Button设置可用性:setVisibility(View.VIS ...

  9. python sqlalchemy-migrate 使用方法

    1:下载相关模块     pip install sqlalchemy     pip install sqlalchemy-migrate   2:创建model (model.py),这里用来绑定 ...

  10. android 根据域名得到IP

    public static String GetInetAddress(String host) { String IPAddress = ""; InetAddress Retu ...