文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Usage of the pointer to pointer

Usage of the pointer to pointer

时间:2009-07-01  来源:edwinrong

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 <stdio.h>
  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 : http://www.daniweb.com/forums/showthread.php?t=69966&page=2&highlight=usage+double+pointer
  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. }
相关阅读 更多 +
排行榜 更多 +
僵尸运行3d城市逃生

僵尸运行3d城市逃生

冒险解谜 下载
顶尖猎人罗迪和凯茜

顶尖猎人罗迪和凯茜

冒险解谜 下载
火柴人飞爪忍者

火柴人飞爪忍者

冒险解谜 下载