Difference between char * A and char a []. Difference Between const char * A and char * const

Source: Internet
Author: User

See the example

# Include <iostream> using namespace STD; void main (void) {// The difference between char * A and char a [] char * A = "abcdef "; // A is a pointer and its value can be changed. Now a points to a constant string cout <A <Endl; A = "ghijkl "; // a now points to another constant string // A [0] = 'Z'; // A points to a constant. Compilation is fine, however, a cout <A <Endl; cout <sizeof (a) <Endl; // 4 char B [] = "abcdef "; // B is the first address of the array and a constant. Its value cannot be changed. The content is not the constant string cout <B <Endl; // B = "ghijkl"; // compilation fails, and B is the constant, you cannot specify other values for B [0] = 'Z'; cout <B <Endl; cout <sizeof (B) <Endl; // 7 // character // difference between const char * A and char * const a char C [] = "ABCD"; char d [] = "efgh "; const char * Pc = C; // PC [0] = 'Z'; // compilation error. PC is a pointer to a constant, make cout <Pc <Endl; Pc = D; cout <Pc <Endl; // inside the content pointed by the PC The content cannot be changed, but it can be changed to Char E [] = "ABCD"; char f [] = "efgh"; char * const Pe = E; // Pe = F; // PE is a constant pointer and cannot be changed to PE [0] = 'Z'; // PE is a constant pointer, although it cannot be changed, however, you can modify the content to point to cout <PE <Endl ;}// Conclusion: (1) char * A in this form: A is a pointer, and you can change its direction, // The string it points to is a constant and cannot be modified. // (2) in the form of char a [], A is an array name, A is a constant, and cannot point to other strings, but the content it points to is not a constant string, // It can be changed. // (1) const char * A is a pointer to a constant and the content is a constant. // It cannot be modified, but can be changed, this form is also equivalent to // char const *. // (2) char * const A is a pointer constant, which cannot be changed but can be // modified. // (3) const char * const A is a constant pointer to a constant, that is, it cannot be // changed or changed.

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.