Chinaunix首页 | 论坛 | 博客
  • 博客访问: 633433
  • 博文数量: 75
  • 博客积分: 7001
  • 博客等级: 少将
  • 技术积分: 1465
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-11 17:39
文章分类

全部博文(75)

文章存档

2010年(1)

2009年(25)

2008年(49)

我的朋友

分类: LINUX

2009-07-01 20:48:40


See the registration functiion of notifier chain:

static int notifier_chain_register(struct notifier_block **nl,
        struct notifier_block *n)
{
    while ((*nl) != NULL) {
        if (n->priority > (*nl)->priority)
            break;
        nl = &((*nl)->next);
    }
    n->next = *nl;
    rcu_assign_pointer(*nl, n);
    return 0;
}

Why the first parameter is of type pointer to pointer, marked with red color?

That's due to the code marked with blue color, the value of parameter nl is expected to be changed, the value of nl is the address of input argument with struct notifier_block, we know that to modified the value of parameter of subroutine, it's supposed to pass reference to the subroutine, herein, we should ship the reference to varible nl, that's the address of nl, namely, the address of a "address", so it adopts double pointers.

Some examples:
eg01:
  1 #include
  2
  3 int b=2;
  4
  5 void test_p(int * pa)
  6 {
  7     pa = &b;
  8 }
  9
 10 void test_pp(int **ppa)
 11 {
 12     *ppa = &b;
 13 }
 14
 15 int main(void)
 16 {
 17
 18     int a=1;
 19     int *p=&a;
 20
 21     printf("value of p is %p, content is %d\n", p, *p);
 22     test_p(p);
 23     printf("value of p is %p, content is %d\n", p, *p);
 24     printf("---------------------------\n");
 25     p=&a;
 26     printf("value of p is %p, content is %d\n", p, *p);
 27     test_pp(&p);
 28     printf("value of p is %p, content is %d\n", p, *p);
 29
 30     return 0;
 31
 32
 33 }

In my PC, the result is as follows:

value of p is 0xbf99f930, content is 1
value of p is 0xbf99f930, content is 1
---------------------------
value of p is 0xbf99f930, content is 1
value of p is 0x804a018, content is 2

eg02 -->
from :
  1. void foo(int**p)
  2. {
  3. int y = 5; //memory for this is allocated, say at address 3000, and that position of memory is filled with the value 5
  4. *p = &y; //we equal the value (contents) at address 2000 to 3000;
  5. /*note that we don't return the address of the int */
  6. }
  7. void main()
  8. {
  9. int* p; //memory for this pointer (not for its contents!) is allocated, say at address 2000
  10. /* p points to nowhere right now */
  11. foo(&p); //we pass 2000 (p's address) to foo()
  12. /* p's value is now 3000, where an integer is stored */
  13. printf("%d",*p); //p is updated, its value (*p) is 5.
  14. }

阅读(1309) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~