C pointer principle (15)-C pointer basis

Simple C pointer Pointer to integer and pointer to pointer myhaspl@myhaspl:~ % vim test1.c #include <stdio.h> int main(void){ int x; x=128; int...

Simple C pointer

Pointer to integer and pointer to pointer

myhaspl@myhaspl:~ % vim test1.c #include <stdio.h> int main(void){ int x; x=128; int *myp=&x; int **mypp=&myp; printf("x:%d\n",x); printf("myp:%u\n",myp); printf("mypp:%u\n",mypp); return 1; }

The above program defines an int type integer, and then defines two pointers, one is myp, the other is mypp.

myp and mypp are pointer variables, but they point to different contents. myp points to the address of X, mypp points to the address of myp, and X can be found through myp, while x cannot be found immediately through mypp. Mypp finds myp first, and then x through myp. Therefore, mypp is also called pointer pointer.

myhaspl@myhaspl:~ % make cc test1.c -o mytest myhaspl@myhaspl:~ % ./mytest x:128 myp:4294957796 mypp:4294957784 myhaspl@myhaspl:~ %

Extract what the pointer points to by the dereference operator *.

myhaspl@myhaspl:~ % ./mytest x:128 myp:4294957796 mypp:4294957784 *myp:128 **mypp:128

Code for

#include <stdio.h> int main(void){ int x; x=128; int *myp=&x; int **mypp=&myp; printf("x:%d\n",x); printf("myp:%u\n",myp); printf("mypp:%u\n",mypp); printf("*myp:%d\n",*myp); printf("**mypp:%u\n",**mypp); return 1; }

*myp extracts the content of x

What * * mypp extracts is also the content of x

So what does * mypp extract

Is the content of the myp pointer variable itself, the address of x.

The above procedures can be modified to verify

#include <stdio.h> int main(void){ int x; x=128; int *myp=&x; int **mypp=&myp; printf("x:%d\n",x); printf("myp:%u\n",myp); printf("mypp:%u\n",mypp); printf("*myp:%d\n",*myp); printf("**mypp:%u\n",**mypp); printf("*mypp:%u-myp%u\n",*mypp,myp); return 1; }

Program execution result: see the last line

myhaspl@myhaspl:~ % ./mytest x:128 myp:4294957796 mypp:4294957784 *myp:128 **mypp:128 *mypp:4294957796-myp4294957796 myhaspl@myhaspl:~ %

4 December 2019, 06:08 | Views: 2122

Add new comment

For adding a comment, please log in
or create account

0 comments