const keyword

const keyword

usage

1. Define constants

const int MAX_VAL = 100;

2. Define a constant pointer

int m = 5;
const int * p = &m;
*p = 4; //error

Notice:

1. The content pointed to by a constant pointer cannot be modified

2. You cannot assign a constant pointer to a non-constant pointer, and vice versa.

unless cast

3. When the function parameter is a constant pointer, it can avoid accidentally changing the content of the place pointed to by the parameter pointer inside the function

void Myprint(const char *p)
{
strcmp(p,"hello");//Error
printf("%s",p);//Correct
}

3. Define often quoted

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325991949&siteId=291194637