int *const && INT const * && const INT * Difference

Source: Internet
Author: User

Ansic allows you to declare constants, constants and variables, constants that cannot be changed, and modifiers with the keyword const

For example: const int A

int const A

The above two declarations are the same, we do not need to consider the order of const and int, according to your understanding of the convenient way to apply.

Because the order of the const and int does not affect the result, the INT const * && const INT * is the same in both cases

So we just need to talk about two things.

------------------------------------------1:int Const *PTR

We said const int A; Is a constant that declares an int, and int * PTR; is a pointer variable that declares an int, so the int const *PTR is a pointer to the shape constant, and we can modify the value of the pointer, but we can't modify the value of *ptr. , which is the value pointed to by the pointer;

1#include <stdio.h>2 intMain () {3     intA=Ten;4     intb= -;5     int  Const* p1=&A;6     //(*P1) = 12; We can modify the value of the pointer, but we can't modify the value he points to.7p1=&b;8printf"%d\n",*p1);9b= -;Tenprintf"%d\n",*p1); One     return 0; A}

In the above program, the value of P1 can be modified to point to the address of another variable, but the value of (*P1) can not be modified

The first const modifier is the entire *pi (note that I write *pi instead of pi). So *pi is a constant and cannot be assigned (although PI refers to B as a variable, not a constant). Second, PI is not a const modifier, so pi is a pointer variable that can be assigned to another memory address.

------------------------------------------2:int * Const PTR

#include <stdio.h>intMain () {intA=Ten; intb= -; int*Constp1=&A; printf ("%d\n",*p1); (*P1) = A; //p1=&b; //We can modify the value he points to, but cannot modify the value of the pointerprintf"%d\n",*p1); return 0;}

We can't change the value of the const-modified P1,P1 here, but we can change the value he points to.

P1 has a const modifier that becomes a pointer constant that cannot be modified

The entire *P1 is not const-modified, so *P1 is a variable, not a constant

-----------------------------3 Conversion Issues

1#include <stdio.h>2 intMain () {3 4     Const intn=Ten;5     int*p;6P= (int*) &N;7printf"%d\n",*p);8*p= A;9printf"%d\n",*p);Ten     return 0; One}
case one: int * pi pointer pointing to const int N constant
const int n=10;
int *p;
p=&n;//, is that OK? No, VC is a compilation error. The address of the const int type n is not
//Assigns a pointer to the int type address of p. Otherwise p is not able to modify the value of n!
p= (int*) &n; Is that OK? Coercion of type conversions is supported by C.
//VC, but still cannot modify the value of N by *p=80. Go and try it! Look at the specifics.

case TWO: const int * p pointer pointing to const INT n
const int n=40;
const int * p;
p=&n;//Two types are the same, you can assign this value. The value of n cannot be modified either by P or N.

Scenario Three: pointer declared with const INT * Const P
int n;
Const INT * Const p=&n;
//Can you imagine what P can do? The P-value cannot be changed, nor can it be modified by the value of N. Because whether it is
//*p or P are both const.

int *const && INT const * && const INT * Difference

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.