Drill down to the difference between the const int *P and int * Const P (constant pointer and pointer to constant) _c language

Source: Internet
Author: User
For pointers and constants, the following three forms are correct:
Copy Code code as follows:

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

The following three types are described in turn.
Because the * operator is the left operator, the precedence of the left operator is from right to left,
1. Constant pointer (Constant pointers)
Copy Code code as follows:

int * Const P

Look at the const first and then look at *, is P is a constant type of pointer, can not modify the pointer, but the pointer to the address stored on the value can be modified.
Example 1:
Copy Code code as follows:

#include <iostream>
#include <stdlib.h>
using namespace Std;
void Main ()
{
int i1=30;
int i2=40;
The int * Const pi=&i1;//Here is the PI pointer-type constant.
pi=&i2; Note here that pi can no longer be assigned this way, that is, you cannot point to another new address. So I have commented on it.
printf ("%d\n", *PI); Output is 30
i1=80; 5. Think about it: can you replace it with *pi=80? OK, here you can modify the I1 value by *PI.
printf ("%d\n", *PI); Output is 80
System ("pause");
}

Example 2:
Copy Code code as follows:

Char char_a = ' A ';
Char char_b = ' B ';

char * Const MYPTR = &char_A;
Myptr = &char_B; Error-can ' t change address of MYPTR

2. Pointer to constant (pointers to Constants)
Copy Code code as follows:

const INT *P

Look at the const first, define a pointer to a constant, and you cannot modify the value that the pointer points to by using the pointer.
Example 3:
Copy Code code as follows:

#include <iostream>
#include <stdlib.h>
using namespace Std;
void Main ()
{
int i1=30;
int i2=40;
const INT * pi=&i1;
printf ("%d\n", *PI); Output is 30
pi=&i2; Note here that PI can reassign a new memory address at any time
i2=80; Think about it: can you use *PI=80 instead? Of course not.
printf ("%d\n", *PI); Output is 80
System ("pause");
}

Instance 4
Copy Code code as follows:

Char char_a = ' A ';
const char * myptr = &char_A;
*myptr = ' J '; Error-can ' t change value of *MYPTR

So the number of integers the pointer p points to is a constant, and the value cannot be modified.
3. Constant pointer to constant
For constant pointers to constants, you must meet the contents of both 1 and 2, neither modifying the value of the pointer nor modifying the value to which the pointer points.
4. Introducing character arrays and character pointers
character arrays and character pointers are defined as follows:
Copy Code code as follows:

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

You can interpret a as a constant pointer, and p is a pointer to a constant, and the code example is as follows:
Copy Code code as follows:

#include <iostream>
#include <stdlib.h>
using namespace Std;
void Main ()
{
Char a[] = "I love you!"; Defines an array of characters, the array name A is a constant pointer to the same position, and is 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, which cannot be modified
* (p+1) = ' A '//error, you cannot modify the value that the pointer points to, so comment it out here.
A[1]= ' a '//constant pointer, you cannot modify the value of the pointer, but you can modify the value that the pointer points to.
a=p;//error, A is a constant pointer and cannot modify its value.
cout<<a<<endl;
cout<<p<<endl;
cout<<a[1]<<endl;
cout<<* (p+2) <<endl;
System ("pause");
}

The output value is:
Ialove you!
I Love you!
A
L

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.