Home > Backend Development > C#.Net Tutorial > Examples of pointer constants and constant pointers

Examples of pointer constants and constant pointers

little bottle
Release: 2019-04-08 12:06:26
forward
2953 people have browsed it

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;
}
Copy after login

Tips

  • Except this In addition, you need to be wary of constant pointers pointing to constants. const int *const p = &q;Neither the pointer nor the value pointed to in the memory can be changed.
  • If there are references in the formal parameters of the function, pay attention to the difference from pointer formal parameters when calling the function to pass parameters.

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!

Related labels:
c++
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template