The difference between the "go" const int *P and the int * Const p (a constant pointer and a pointer to a constant)

Source: Internet
Author: User

"Turn" Xwdreamer Source: Http://www.cnblogs.com/xwdreamer

For pointers and constants, the following three forms are correct:

const char * myptr = &char_a;//pointer to constant char * Const MYPTR = &char_a;//constant Pointer const char * CONST MYPTR = &char_a;/ /constant Pointer to constant

These three types are described in turn below.

Because the * operator is the left operator, the priority of the left operator is right-to-left, for

1. Constant pointer (Constant pointers)
int * Const P

First look at the const and then see *, is P is a constant type of pointer, cannot modify the pointer point, but the pointer to the address stored on the value can be modified.

Example 1:
#include <iostream>#include<stdlib.h>using namespacestd;voidMain () {inti1= -; intI2= +; int*Constpi=&i1;//Here's the PI pointer-type constant. //pi=&i2; //Note here that the PI can no longer be re-assigned, that is, it can no longer point to another new address. So I've commented on it. printf"%d\n", *PI);//the output isi1= the;//5. Think about it: Can you use *pi=80 here instead? Yes, the value of I1 can be modified here by *PI. printf"%d\n", *PI);//the output isSystem"Pause");}

Example 2:
Char ' A ' ; Char ' B '  charconst myptr = &= &char_B;    // Error-can ' t change address of Myptr

2. Pointer to constant (pointers to Constants)
const INT *P

First Look at * and then look at the const, define a pointer to a constant that cannot be modified by a pointer to the value pointed to by the pointer.

Example 3:
#include <iostream>#include<stdlib.h>using namespacestd;voidMain () {inti1= -; intI2= +; Const int* pi=&I1; printf ("%d\n", *PI);//the output ispi=&i2;//Note here that PI can re-assign a new memory address at any timeI2= the;//Think about it: Can you use *pi=80 here instead? Of course not .printf"%d\n", *PI);//the output isSystem ("Pause");}
Example 4:
Char char_a = ' A '; const char * myptr = &char_a;*myptr = ' J ';    Error-can ' t change value of *MYPTR

So the integer number that the pointer p points to is a constant whose value cannot be modified.

3. Constant pointer to constant

For constant pointers to constants, the contents of 1 and 2 above must be met, neither the value of the pointer nor the value pointed to by the pointer can be modified.

4. Introduction of character arrays and character pointers

The character array and the character pointer are defined as follows:

Char a[] = "I love you!"; Defines a character array char *p = "I love you!";  Defines a character pointer

A can be understood as a constant pointer, and p is a pointer to a constant, and the code example is as follows:

#include <iostream>#include<stdlib.h>using namespacestd;voidMain () {CharA[] ="I Love you!";//defines a character array, where the array name A is a constant pointer, and the position of the pointer is the same as the position of the first element of the array .    Char*p ="I Love you!";//A character pointer is defined, and the pointer p points to a string constant that cannot be modified by a string constant .//* (p+1) = ' a ';//error, you cannot modify the value pointed to by the pointer, so comment it out here. a[1]='a';//constant pointer, you cannot modify the value of a pointer, but you can modify the value that the pointer points to. //a=p;//error, A is a constant pointer, and its value cannot be modified. cout<<a<<Endl; cout<<p<<Endl; cout<<a[1]<<Endl; cout<<* (p+2) <<Endl; System ("Pause");}

The output values are:

Ialove you!
I Love you!
A
L

The difference between the "go" const int *P and the int * Const p (a constant pointer and a pointer to a constant)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.