Const usage Summary

Source: Internet
Author: User

Const usage Summary

 

The const qualifier is characterized by the principle of forced minimum access. Correct software design with this principle can greatly reduce debugging time and adverse side effects, and make the program easy to modify and debug.

Minimum Access Principle: to complete a known task, a function is always granted sufficient permissions to access its parameter data, but no more permissions are granted.

The pointer passed to the function can be in the following four situations:
A constant pointer pointing to a very large volume of data, a constant pointer pointing to a very large volume of data, a constant pointer pointing to a constant data

1) if the value passed to the function is not (or should not) modified in the function body, use const to declare the value to prevent accidental modification of the value.
If you print a one-dimensional array but do not want to change the length value of the array in the subroutine, you can use const to modify the value.

2) A very large pointer pointing to a very large volume of data has the highest access permission. In this case, the pointer can be reused to modify data and be able to access other data items by modifying the pointer.

3) You can modify the extraordinary pointer to the constant data so that it points to any data item of the appropriate type, but the data it points to cannot be modified.
Example :'
Void printcharacters (const char * s)
{
For (; * s! = '/0'; s ++)
Putchar (* s );
}
This pointer can be used to accept an array parameter, and then let the function process every unit of the array without modifying the data.
You can use a pointer to the constant data to pass a large object with the same structure to obtain the performance of passing reference calls and the security of passing value calls.

If the memory is sufficient and the execution efficiency is a concern, you should use the pass-through call to transmit data to enforce the principle of least access. You must know that some systems do not support const very well. Therefore, it is the best way to protect data from modification during value transfer.

4) A constant pointer pointing to a very large amount of data always points to the same memory, where data can be modified through this pointer. This is the case with the array name.

 

 

Question: Only one value can be modified in the call function for value passing. To modify multiple values in a call function, you must use the pass reference to call ???

 

From C Language Programming Tutorial C how to program

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.