Take a small example to see what the C language pointers p, *p, &p, *&p, &*p represent

foreword

In C language, pointer is a very important concept. A pointer is a variable whose value is the address of another variable. Data in memory can be directly accessed using pointers, which makes the C language very flexible and powerful. When learning C language, everyone already knows the difference between &and , but do you know what and represent?**&p&*p

write a test program

The following is a simple C language code that can be used to demonstrate the several identifiers mentioned above, so that we can better understand the meaning of several identifiers:

#include <stdio.h>

int main() {
    
    
    int a = 10;
    int *p = &a;
    int **pp = &p;

    printf("a=%d\n", a); // 打印a的值
    printf("&a=%p\n", &a); // 打印a的地址
    printf("p=%p\n", p); // 打印p的值,即a的地址
    printf("*p=%d\n", *p); // 打印p所指向的变量的值,即a的值
    printf("&p=%p\n", &p); // 打印p的地址
    printf("*pp=%p\n", *pp); // 打印pp所指向的指针变量p的值,即a的地址
    printf("**pp=%d\n", **pp); // 打印pp所指向的指针变量p所指向的变量的值,即a的值
    printf("&pp=%p\n", &pp); // 打印pp的地址
    printf("*&p=%p\n", *&p); // 打印p的值,即a的地址
    printf("&*p=%p\n", &*p); // 打印p的地址

    return 0;
}

Let's run this program and see the output:

a=10
&a=0x7fff87a4ce94
p=0x7fff87a4ce94
*p=10
&p=0x7fff87a4ce98
*pp=0x7fff87a4ce94
**pp=10
&pp=0x7fff87a4cea0
*&p=0x7fff87a4ce94
&*p=0x7fff87a4ce94

This program defines an integer variable a, a pointer p to the integer variable, and a pointer pp to the pointer variable p. The program outputs the values ​​or addresses represented by the several identifiers mentioned above. Run the program and see its output to better understand what these identifiers mean.

From the above running results, it can be seen that *pthe printed avalue is the value of the variable, that is, pthe value of the variable. This is beyond doubt, and everyone must know it. Let's take a look at what the other variables mean!

  • p: Pointer variable name. When defining a pointer variable, it is necessary to specify the data type of the variable pointed to by the pointer. For example, int *p; defines a pointer variable to a variable of type int p.

  • *p: The dereference operator. It is used to access the value of the variable pointed to by the pointer. For example, *p = 10; assigns 10 to the variable pointed to by p.

  • &p: Take address operator. It is used to get pthe address of a pointer variable. For example, a pointer variable that int *p; int **pp = &p;points to a pointer variable is defined .ppp

  • *&p: This operator is equivalent to p. It will first ptake the address and then dereference it to get pits own value.

  • &*p: This operator is equivalent to p. It first pdereferences to get pthe value of the variable pointed to, and then takes its address to get pits own address.

summary

  • aand *poutputs athe value of the variable.
  • &apThe output of and is athe address of .
  • &*pand are *&pactually pboth, the address aof .

Guess you like

Origin blog.csdn.net/qq_45172832/article/details/130237944