Use const in C to protect the notes of array content

Source: Internet
Author: User

For example: int sum (const int ar [], int n)

If const int days [MONTHS] = {31, 28, 31}, the array content cannot be changed.

If:

Double rates [5] = {88.9, 100.12, 59.45, 183.11, 340.5 };
Const double * pd = rates;

You cannot use the pc to modify the value it points to. However, you can still modify the value through rates [0] because rates is not a constant.

If we change the writing method to double * const pc = rates, we can modify the data but cannot move the pointer.

If the above two are used in combination, it is equivalent to the use of strong glue, how can not be moved:

Const double * const pc = rates.

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.