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 : 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.
}
阅读(1347) | 评论(0) | 转发(0) |