免费注册 查看新帖 |

Chinaunix

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

Usage of the pointer to pointer [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-07-01 20:48 |只看该作者 |倒序浏览

               
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 :
http://www.daniweb.com/forums/showthread.php?t=69966&page=2&highlight=usage+double+pointer
void foo(int**p){    int y = 5; //memory for this is allocated, say at address 3000, and that position of memory is filled with the value 5    *p = &y; //we equal the value (contents) at address 2000 to 3000;    /*note that we don't return the address of the int */}void main(){    int* p; //memory for this pointer (not for its contents!) is allocated, say at address 2000    /* p points to nowhere right now */    foo(&p); //we pass 2000 (p's address) to foo()    /* p's value is now 3000, where an integer is stored */    printf("%d",*p); //p is updated, its value (*p) is 5. }
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/43047/showart_1983340.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP