Gotchas 31-misunderstanding about type conversion of pointer types whose target type is a constant

Source: Internet
Author: User

First, we need to clarify several terms: const pointer and pointer to output:

A const pointer is a pointer that has a constant worth it. It does not include any hint of "whether it is a constant.
Const char * PCI; // pointer to a constant
Char * const CPI = 0; // constant pointer
Char const * pci2; // still a pointer to a constant
Const char * const CPCI = 0; // constant pointer to a constant.
Char * IP; // common pointer

"C ++ standards allow for the (unconditional) type conversion of the 'increase constant ". For example, we can copy a pointer pointing to a constant to a pointer pointing to a constant. In this way, we can -- of course, we can also do many other things -- pass a very large number pointer pointing to the char type to the strcmp or strlen function in the standard library, although their declaration only accepts pointers to Char constants, intuitively we can understand why: allow pointing to constants to point to constants rather than constants, because we do not lose any constraints in data declaration, of course, we can also understand why reverse conversion is blocked, because we get more permissions when data is declared;
Size_t strlen (const char *);
//...
Int I = strlen (CPI); // No problem parameter transfer
PCI = IP; // No problem with pointer assignment
IP = PCI; // Error
Note that C ++ adopts a conservative opinion on data constants: in fact, this can also be done -- without immediately causing a core dump -- we can modify the data that points to the constant pointer, as long as the data is actually (in the unspecified semantics) not constant ①, or they are constants, but the platform that runs the Code does not allocate data to the read-only Area of the internal memory. In short, the typical use of constants is a stand expression during the design period, and also a system attribute identifier. C ++ seems to force the designer's intent.
========================================================== ==================================
Note:
① Annotation: that is to say, if a pointer to a constant points to a data that itself is not a constant, then this pointer to a constant cannot be used to modify the data, but it does not indicate that this data cannot be modified. → See common errors 6.

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.