2

Summary

1) The essence of the pointer is a variable. The special that the content stored in 161d0262cbdaf1 is a memory address.

2) When the pointer declared, * indicates that the declared variable is a pointer; when the pointer used, * indicates the value memory space pointed to by the pointer

3) Pointer as a function parameter:

  • The pointer is a variable, so you can declare the pointer parameter
  • When a function body needs change the value of the actual parameter
  • You need to use pointer parameters; the will be copied to the formal parameter when the function is called
  • Pointers are suitable for functions with complex data types as parameters

4) : 161d0262cbddf5 left and right-when const appears on the *, the data pointed to by is constant; when const appears on the *, the pointer of is constant.

const int* p;        // 指针p可变,指向的数据*p不可变
int const* p;        // 指针p可变,指向的数据*p不可变
int* const p;        // 指针p不可变,指向的数据*p可变
const int* const p;  // 指针p不可变,指向的数据*p也不可变

Analysis of the nature of pointers

variable in the program is just an alias for a section of storage space of , so do I have to use this alias to use this storage space?

1. The declaration and use of pointers

What does the following program output? Why

int i = 5;
int* p = &i;

printf("%d, %p\n", i, p);    // 5, 0XBFDB216C

*p = 10;

printf("%d, %p\n", i, p);    // 10, 0XBFDB216C
  • When the pointer declared, * indicates that the declared variable is a pointer
  • When the pointer used, * represents the value memory space pointed to by the pointer
    image.png

2. Call by value and call by address

  • The pointer is a variable, so you can declare the pointer parameter
  • When a function body needs change the value of the actual parameter, you need to use the pointer parameter
  • When the function is called, the will be copied to the formal parameter
  • Pointers are suitable for complex data types of 161d0262cbe2be as parameters

    // 指针作为参数分析
    void swap1(int a, int b)
    {
      int t = a;
      a = b;
      b = a;
    }
    
    void swap2(int* pa, int* pb)
    {
      int t = *pa;
      *pa = *pb;
      *pb = t;
    }
    
    int main()
    {
      int a = 1, b = 2;
    
      swap1(a, b);                 // 将实参a和b的值,赋值给swap1参数a、b,函数内部实际修改的是函数的形参a和b,不会影响实参
      printf("%d, %d\n", a, b);    // 1, 2
      
      swap2(&a, &b);               // 将实参a和b的地址,赋值给参数指针pa和pb,函数内部使用*号打开了这两个地址,并修改了里面的值,所以外面的实参也会变
      printf("%d, %d\n", a, b);    // 2, 1
      return 0;
    }

3. const and pointer

Formula: left and right points - When const appears on the left side of *, the data pointed to is constant; when const appears on the right side of *, the pointer is constant.

const int* p;        // 指针p可变,指向的数据*p不可变
int const* p;        // 指针p可变,指向的数据*p不可变
int* const p;        // 指针p不可变,指向的数据*p可变
const int* const p;  // 指针p不可变,指向的数据*p也不可变

This article is summarized from "C Language Advanced Course" by Tang Zuolin from "Ditai Software Academy".
If there are any errors or omissions, please correct me.


bryson
169 声望12 粉丝