[Expand knowledge 5] Talk about const and const.

Source: Internet
Author: User

[Expand knowledge 5] Talk about const and const.

[Expand knowledge 5] Talk about const-related things

 

Extension directory:

1. const modifier variable

2. conts modifier Parameters

3. const modifier function return value

 

(1) Purpose of const

Const is the abbreviation of constant, meaning constant. However, the variable modified in C language is a read-only variable, and its value cannot be used during compilation because the compiler does not know its stored content during compilation.

 

Purpose: To replace precompiled commands, that is, to eliminate the disadvantages of pre-compilation and inherit its advantages. (Const has replaced the pre-compiled instruction position in C ++)

Generally, the compiler does not allocate storage space for common cconst Read-Only variables, but stores them in the symbol table.

It becomes a value during compilation, without the storage and read memory operations, saving space, avoiding unnecessary memory allocation, and improving efficiency.

 

[Program 1]:

 

// Function: to test whether the value of the const variable cannot be changed or the number of elements needs to be determined by defining the array # include <stdio. h> int main (void) {constint n = 100; // cosnt modifies n, so n is read-only and cannot be changed. // an error is returned !! // Because the number of elements (I .e. constants) must be determined when defining an array // It is proved that the n modified by const is not a constant intarray [n] = {0 }; // define n integer arrays and initialize them to 0. // when we change the value of n, the compiler reports an error: n is a read-only variable // n = 200; return0 ;}


[Procedure 2]

// The test case statement must be a constant value # include <stdio. h> int main (void) {constint n = 4; // n is not a constant, but read-only int num = 4; switch (num) {// The first case statement reports an error: caseexpression not constant // Verify again that the const modified n is not a constant casen: {printf ("n = 4 \ n"); break;} case3: {printf ("n = 3 \ n"); break;} // omitting .... default: {printf ("n = Other \ n"); break ;}} return 0 ;}


(2) const variable Modification

(A) cosnt modifier for general Variables

 

(B) cosnt modifier pointer variable

Const int * p; // the pointer Variable p is variable. The object (content) pointed to by p is immutable. int const * p; // the pointer Variable p is variable, and the object (content) pointed to by p is variable) immutable int * const p; // the pointer Variable p is immutable, and the object (content) pointed by p is variable const int * const p; // The object (content) pointed to by the pointer variables p and p cannot be changed.


 

How to read? (Reference), method: Remove type

Const int * p; // const modifier * p, so it is immutable; p is a pointer, not modified by const, variable int const * p; // const modifier * p, so it is immutable. p is a pointer and is not modified by const. The variable int * const p; // cosnt modifies the pointer p, which is immutable. * p is not modified, therefore, the variable const int * const p; // const modifies * p and p, so both are immutable.


(3) const modifier function parameters

The cosnt parameter cannot be changed!

For example, the strcpy program (the exclusive function) prevents strSrc from being modified and uses the const restriction.

Char * strcpy (char * strDest, const char * strSrc) {// determine whether the following expression is true. If not, exit assert (strDest! = NULL) & (strSrc! = NULL); // asserted char * address = strSrc; // save strSrc // one-to-one value assignment while (* strDest ++ = * strSrc ++ )! = '\ 0 ')//! = The priority is higher than = {; // empty statement} return address; // return the original character strSrc, supporting chain expression}


(4) cosnt modifier function return value

The Return Value of the const modifier function is vice versa.

 

[Smile at your fingertips] errors are inevitable. I hope you can get your corrections ^-^

Reprinted when retaining the original link http://codingit.howbbs.com and http://blog.csdn.net/mirrorsbeyourself

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.