Difference between const char * and char * const

Source: Internet
Author: User

1. pointer to constant = pointer to a constant. The content to which the Pointer Points cannot be changed, but the pointer value can be changed.
Char ch [5] = "Lisi ";
Const char * pstr = CH; // pointer to a constant, which can be defined first and then assigned a value.

Pstr = "ABCD"; // you can assign a value to the pointer to point to another constant.
* Pstr = "F"; // It is not allowed to change the content pointed to by the pointer.

In this way, you cannot mistakenly modify the pointer, which is often used as the form parameter of the function.

2. pointer constant = the pointer itself is a constant, and the content to which it points can be changed, but the pointer value cannot be changed.
Char ch [5] = "Lisi ";
Char * const pstr = CH; // pointer constant, which must be assigned at the same time as defined

Pstr = "ABCD"; // It is not allowed to assign a value to the pointer to point to another constant.
* Pstr = "F"; // you can change the content pointed to by the pointer.

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.