Const usage in C Language

Source: Internet
Author: User

1. Common usage of const
Const int n = 10;
Obviously, n is a read-only variable, and the program cannot directly modify its value. Note that int a [n]; in ansi c, this method is incorrect because the array size should be a constant, n is just a variable.

 

2. const is used for pointer
Const int * p;
Int const * p;
Int * const p;

In the last case, the pointer is read-only (p read-only). In the other two cases, the Pointer Points to an object that is read-only (* p read-only ). Const is a type modifier that is left-bound. It is a type modifier along with the type Modifier on the left. Therefore, int const is limited to * p, not limited to p. Int * const is limited to p, not limited to * p.

 

Here is a simple differentiation method: draw a line along the *. If the const is on the left side of *, the const is used to modify the variable pointed to by the pointer, that is, the pointer points to a constant; if const is on the right side of *, const is to modify the pointer itself, that is, the pointer itself is a constant.

 

3. const is used for function address transfer parameters.
Void foo (const int * p );
This form is usually used to simulate value transfer calls in parameters in the array form. That is equivalent to the function caller's claim: "I will give you a pointer to it, but you cannot modify it. "If function writers follow this convention, it is equivalent to simulating value transfer. This is also the most useful feature of const: it is used to limit the shape parameters of a function, so that the function will not modify the data referred to by the real parameter pointer. Note that the function should not be modified, rather than cannot be modified. That is to say, the const cannot prevent parameter modification (see the cause ).

 

4. const is used to limit the return value of a function.
Const int foo ();
Const struct mytype foo ();
The preceding statement limits that the return value of a function cannot be updated. When the function returns an internal type, it is already a numeric value. Of course, it cannot be updated by a value assignment. Therefore, const is meaningless at this time and it is best to remove it, to avoid confusion. When the function returns a custom type, this type still contains variable members that can be assigned values. Therefore, this makes sense.

 

This article is from the "Sun's technology blog" blog, please be sure to keep this source http://sunke.blog.51cto.com/4812218/1282573

Related Article

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.