Const and pointer in c ++

Source: Internet
Author: User

In c/c ++, pointer itself is a difficult point. In addition, the combination with const often produces many confusing points. Here we will make a summary.

1. Define a const object
Const int buffsize = 512;
Since the constant definition cannot be modified, it must be initialized during definition.
Const I, j = 0; // error, I not initialized

2. the const object is the local variable of the file by default.
// File1.cc
Extern const int bufsize = 512; // define and initialize

// File2.cc
Extern const int bufsize; // Declaration
For (int index = 0; index! = Bufsize; ++ index)
{
//...
}

The default value of a non-const variable is extern. To make the const variable accessible in other files, it must be specified as extern.

3. pointer to the const object

Const int * p;
This p is a pointer to an int-type const object. const limits the type pointed to by the pointer p rather than p itself. That is, p

It is not a const. You do not need to initialize it during definition. You can also assign a value to p to point it to another const object.

However, you cannot use p to modify the value of the object to be pointed.
Example 1: int a = 0; p = & a; yes.
Example 2: * p = 20; No.
Conclusion: This pointer to a const object only limits the number of objects that p points to, rather than the object to which p points.

It is not acceptable to assign the address of a const object to a pointer that does not point to a const object.
Example 3: const int B = 10;
Int * p2 = & B; // error
Const int * p3 = & B; // OK
Conclusion: Variable B cannot be modified because it has const modification. However, pointer p2 is a common pointer and can be used to modify the value pointing to an object.
It is illegal to declare a conflict. The pointer to the const object cannot modify the value of the pointer to the object. Therefore, this method is valid.

You cannot use the void * pointer to save the address of the const object, but you must use the const void * type to save the address of the const object.

Const int a = 6;

Void * p = & a; // error

Const * cp = & a; // OK

Int const * p;
C ++ requires that the const keyword is equivalent before the type or variable name.

Const int n = 5; // same as below
Int const m = 10;

Const int * p; // same as below const (int) * p
Int const * q; // (int) const * p

4. const pointer

Int c = 20;
Int * const p4 = & c;

Pointer p4 is called a const pointer. It is the opposite of a pointer to a const object. It cannot modify the pointer to an object, but can modify the pointer to an object.

The value of the forward object. In addition, this pointer must be initialized during declaration.

5. const pointer to the const object

Const int d = 30;
Const int * const dp = & d;

The pointer dp can neither modify the object to which it points, nor modify the value of only the object.

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.