Proximity principle-Use of the const keyword

Source: Internet
Author: User

In C, the purpose of the const keyword is to keep the variable value modified during function operations. We usually see the following const usage, which seems to be summarized as "proximity principle": that is, the variable closest to the right of const indicates that the variable is a constant ."

Const usage in C Language
 
 

Read the skip_atoi () function code in linux0.11, as follows:

static int skip_atoi(const char **s)
{
    int i = 0;

    while (is_digit(**s))
         i = i * 10 + *((*s)++) - '0';

    return i;
}

The function is clear, that is, to convert a numeric string (alpha) to an integer (INT. However, the parameter type passed to skip_atoi () is somewhat confusing. There is no understanding of the pointer to the pointer, the key lies in whether the const modifies S, * s, or ** s in the parameter?

In C, the purpose of the const keyword is to keep the variable value modified during function operations. We usually see the following const usage, which seems to be summarized as "proximity principle": that is, the variable closest to the right of const indicates that the variable is a constant ."

Const int * P1;/* the int variable value pointed to by P1 cannot be changed, which is a constant, but can change the value of the P1 pointer */
Int * const P2;/* P2 pointer is a constant, that is, the P2 value cannot be changed, but P2 can be changed to the object value */
Const int * const P3;/* the P3 pointer is a constant, and the value pointed to by P3 is also a constant */

As the saying goes, everything is well-founded, and it is no exception here. Finally, find the related description from K & R the C programming language (2nd:

A.8.6.1 pointer declarators

In a declaration T d Where D has the form

*Type-qualifier-listOPTD1

And the type of the identifier in the Declaration t d1 is''Type-ModifierT, ''the type of the identifier of D is''Type-modifier type-qualifier-listPointer to T. ''qualifiers following * apply to pointer itself, rather than to the object to which the Pointer Points.

We can see from the above that the Declaration is defined by the "*" and parsed:

For example, const char ** S;

The parsing is as follows:

It is defined by the first number on the rightmost side, in the form of const char ** | S. Since there is no modifier before S, the pointer value can be changed; S is the pointer to the const char * type object. In fact, const char * is the pointer, so s is the pointer to the pointer;

The second number * is defined as const char * | * s. The value can be changed because there is no modifier before * s;

Finally, we will parse the const char * object, which is similar to the const int * P1 mentioned earlier. Therefore, the pair pointed to by this pointer (* s) (here it is a string) is a constant, its value cannot be changed;

Similarly, if the statement is as follows:

Const char ** const S;

Parsing it is only because the modifier const is added before the S pointer, so the S pointer itself is a constant and the pointer value cannot be changed. other meanings are described above (2) (3.

Finally, the test code is provided:

int main()
{
    int a1 = 1;
    int a2 = 2;    
    const int *p1 = &a1;
    int * const p2 = &a2;
    const char *fmt = "hello";
    const char **s = &fmt;

     p1 = &a2;    
    *p1 = a2;    // error

    p2 = &a1;    // error
    *p2 = a1;  
    (*s)++;
     s++;
    (**s)++;    // error

    return 0;
}

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.