Home > Article > Backend Development > Examples of pointer constants and constant pointers
pointer Pointer constants and constant pointers
Both pointer constants and constant pointers are essentially pointers, so what they need to assign is an address.
Many times when using pointers for output, the address of the pointer is always output, and we often forget to output the content of the pointer address.
const int * or int const * are both pointer constants. How to write constant pointers? The constant pointer is written * between int and const, that is: int *const
For example:
#include <iostream> using namespace std; int main(int argc, const char * argv[]) { int b=3; int c=4; int e=40; int f=80; //指针常量:指的是一个指针指向一个常量 const int *q =&b; cout<<*q<<endl; q=&c; cout<<*q<<endl; //*q=5;//可以改变指针的方向,但是指向的地址的值无法修改。 //常量指针----本质是个指针,但是这个指针是常量的,意味着你是不可以随便的就可以更改指针的指向的。 int *const p=&e; cout<<*p<<endl; //输出指针指的内容 //换个指向,让他指向f //p=&f; // cout<<*p<<endl; //给他换个值 *p=90; //这个语句是错误的 也就是说也是指针常量 cout<<*p<<endl; return 0; }
const int *const p = &q;
Neither the pointer nor the value pointed to in the memory can be changed.
The above is the detailed content of Examples of pointer constants and constant pointers. For more information, please follow other related articles on the PHP Chinese website!