Const and pointers

Source: Internet
Author: User

1, there are two cases: a pointer to a const object, const on the left side of *, cannot modify the point of the content. const pointer, const on the right side of *, cannot modify pointer pointing.

2, when assigned to the pointer, you can narrow the permissions, but not to enlarge the permissions. In other words, a non-const object can be assigned a pointer to a const object. However, the Const object cannot be assigned a pointer to a non-const object, error:cannot convert from ' const int * ' to ' int * '. The same is true for references.

3, note: The pointer itself is a value, the whole copy. In other words, a const pointer can be assigned to a non-const pointer, and a non-const pointer can also be assigned to a const pointer. The pointers on both sides are not related, just the same value.

4, however, a pointer to a pointer, or a reference to a pointer, can also reduce permissions, but not to magnify permissions.

5, note: There is a reference to the pointer, that is, *&, but there is no pointer to the reference, that is, &*, why?

Semantics are unreasonable because pointers to references are pointed. A reference is an alias to an object, which points to an object's alias, that is, to an object, so the reference pointer does not exist.

6, consider char* AAA = "Andy"; "Andy" is a literal string constant, assigned to the static store, not allowed to modify the content, its return value is a pointer to the const object, the value of the pointer is the first character address. You can modify the point of the AAA, but you cannot point to the content. Modify the point of the content, compile Yes, run an error. Then the question comes, the right is the const char*, the left is char*, why compile not error? It can be considered to be compatible with previous code, and there is a large number of such code in C code. However, we try to write the const char* AAA = "Andy"; In this case, the run-time error is advanced to the compile time.

7. Consider char bbb[] = "Bill"; As you can understand, allocate 5 bytes on the stack, store ' b ', ' I ', ' l ', ' l ', ' s ', assuming that the address on the stack of ' B ' is 0x12345678,bbb is the alias of this address. BBB is a const pointer that can modify the content. Can not modify the point, if modified, such as bbb++, compile error:' + + ' needs L-value

Const and pointers

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.