Thorough understanding-pointer constants and constant pointers, constant pointers to constants

Preface

  In Chinese, the attributive is generally placed before the head word, especially in technical languages ​​like C and C++.
  So the attributive is important or the head is important, and it must be the head.

  Such as: beautiful girl, beautiful is the attributive girl is the central word
  girl is definitely important, is the essence.

  In the same way, let's not talk about technology first, and understand it literally: pointer constant, constant pointer, function pointer, pointer function, array pointer, pointer array

Pointer constant The pointer is the attribute, and the constant is the central word, so the pointer constant is essentially a constant
Constant pointer The constant is the attributive, the pointer is the central word, so the constant pointer is essentially a pointer
Function pointer Essentially a pointer, that is, a pointer to a function
Pointer function Essentially a function, but the type of the function is a pointer
Array pointer Essentially a pointer, just a pointer to an array
Pointer array Essentially an array, but the array elements are all pointers

1. Pointer constant-pointer type constant (int * const p)

Keep in mind: The role of const is to limit the modifier to a constant, so whoever modifies by const is a constant.

Pointer constant (the pointer itself is a constant)

Definition: The essence is a constant, and it is decorated with a pointer. The value of a pointer constant is a pointer. Because this value is a constant, it cannot be assigned.

key point:

  1. It is a constant!
  2. The pointer itself is a constant, and the pointed address cannot be changed, but the content corresponding to the pointed address can change;
int* const p;

Vernacular explanation:

  Essentially a constant, a pointer is used to describe the type of the constant, indicating that the constant is a constant of the pointer type. In the pointer constant, the value of the pointer itself is a constant, unchangeable, and always points to the same address. Must be initialized while defining. The usage is as follows:

int a = 10, b = 20;
int * const p = &a;
*p = 30;      // p指向的地址是一定的,但内存地址空间中的内容可以修改

2. Constant pointer-pointer to "constant" (const int *p, int const *p)

Remember: the constant is before the pointer, and the programming language: const is before the asterisk.

Definition: Also called constant pointer, it can be understood as a constant pointer, which points to a constant

key point:

  1. The object pointed to by the constant pointer cannot be modified by this pointer, but it can still be modified by the original declaration;
  2. A constant pointer can be assigned to the address of a variable. The reason why it is called a constant pointer is to restrict the value of the variable through this pointer;
  3. The pointer can also point to other places, because the pointer itself is just a variable and can point to any address;
int const* p;  const int* p;

A constant pointer is essentially a pointer. The constant represents the content pointed to by the pointer, indicating that the pointer points to a "constant". In the constant pointer, it is restricted to modify the value of the variable through this pointer, and the pointer seems to point to a constant. The usage is as follows:

int a = 10, b = 20;
const int *p = &a;
p = &b;    // 指针可以指向其他地址,但是内容不可以改变

Judging the right or wrong of the program:

int main()
{
    
    
char * str = “apple”;
str = “orange”;
cout << str << endl;
getchar();
}

This can be compiled and passed on vs2015, but it cannot be compiled on vs2017 and vs2019.
Analysis: Because "apple" is stored in the global (static variable) area, it is read-only and cannot be modified. Therefore, the pointer to the read-only constant must be declared as a constant pointer before it can be compiled.

int main()
{
    
    
   const char * str = "apple";
    str = "orange";
    cout << str << endl;
    getchar();
}

This writing can be compiled and passed on vs2017 and vs2019.

3. Constant pointer to constant

Definition: A pointer constant that points to a constant is a constant, and the object it points to is also a constant.

key point:

  1. A pointer constant points to a pointer object;
  2. The pointer it points to is a constant, that is, the object it points to cannot change;
const int* const p;

statement:

Organized from the blog: Basic knowledge of C/C++ language , welcome to visit the original blog

Guess you like

Origin blog.csdn.net/weixin_43283397/article/details/109387374