What is the usage of const?

coldplay.xixi
Release: 2020-06-20 11:38:58
Original
4259 people have browsed it

What is the usage of const?

What is the usage of const?

# The usage of const is:

1. The common usage of const

int const size;
const int size;
Copy after login

These two statements are Declare size as an integer, and its value cannot be modified.

You can initialize it when declaring it.

int const size =10;
Copy after login

Incorrect usage:

const int size =10;
char buffer[size];  
Copy after login

The error is that size occupies a certain memory block and the C compiler does not know that it is compiling value. Moreover, C defaults to const being externally connected, so it is okay to write it as const.

2.const is used for pointers

(1)

const int *p;
int const *p;         //含义是指向常量的指针,指针的常量值不可以修改
Copy after login

Both are the same, p is a pointer to an integer that is always on and can be modified The value of a pointer, but the value pointed to by the pointer cannot be modified.

int b = 5;
int c = 3;
const int *p=&b;
*p=6;    //错误,不能通过指针修改所指向的值;
p=&c;    //正确,可以修改指针的值 
Copy after login

(2)

int * const p;   //含义是常量指针,p的值不可以修改
Copy after login

p is a pointer, and pointer p is a constant. Its value cannot be modified, but the value of the integer pointed to by p can be modified.

int b = 5;
int c = 3;
int *const p = &b;
*p = 6;  //正确,可以修改指针所值向的值
p =&c;   //错误,不可以修改指针的值 
Copy after login

(3)

const int * const p;
Copy after login

Both the pointer itself and the value it points to are constants.

Simple method of judgment:

If const is on the left side of *, then const is used to modify the variable pointed by the pointer, that is, the pointer points to a bit constant;

If const is located on the right side of *, then const modifies the pointer itself, that is, the pointer itself is a constant.

3.const is used for formal parameters of functions

void foo(const int * p)
Copy after login

When const is used for formal parameters, it means that the formal parameters will not be used inside the function. Change. This form is usually used to simulate call-by-value in array-form parameters. This is also the most useful thing about const, limiting the formal parameters of a function so that the function will not modify the data pointed to by the actual parameter pointer. What needs to be noted here is that the function should not be modified, not that it cannot be modified.

4. Other ways to declare constants

In C language, you can use enum type and #define macro to define constants

#define MAX_SIZE 10;
int const max_size =10;
Copy after login

Used here# define is better than using const variables.

The former can be used wherever literal constants are allowed, such as declaring the length of an array.

const variables can only be used where variables are allowed.

Recommended tutorial: "cVideo Tutorial"

The above is the detailed content of What is the usage of const?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!