[Daily one C 9] C const usage

Source: Internet
Author: User

Daily use of C-language constEvery day, I picked up a C-language shell, which grew up and formed thousands of miles.
Today's shells: C-language const can improve program security and reliability to a certain extent.
1. const modifies the read-only variable: name of the variable of the const Data TypeFor example, const int a = 10;After the const is modified, the variable becomes a read-only variable, which means that the variable must be initialized at the time of definition. Const Read-Only variables, any attempt to modify the read-only variables will cause an error.2. const modifier pointer: as shown in FigureConst int * p; // (* p) (that is, the variable P points to) is immutable, that is, it points to a constant, which is called a constant pointer.Int*Const P; // p is immutable. This pointer is a constant calledPointer constantExample:Constant pointer: Refers to a constant, which can be modified.Pointer constant: A pointer of a constant. The pointer itself cannot be modified.3. const modifier function parameters:When the parameter of a function is a pointer (the parameter is not a pointer and there is no security issue), it is considered as a security concern. If you do not want the function to modify its content, you can use the const modifier to ensure that the function will not modify the parameter.Void foo (const int * p); // The content pointed to by p in the function will not be changed.
Have a nice day!

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.