Advanced usage of const

This article mainly shows the usage of constant array, constant pointer and pointer to constants.

const int days[MONTHS] = {31,28,31,30,31,30,31,31,30,31,30,31};   
days[9] = 44;    /*Compile error */ 

//A pointer to a constant cannot be used to change the value of the data it points to. For details, refer to the following code:
double rates[5] = {88.99, 100.12, 59.45, 183.11, 340.5};
const double * pd = rates;    // pd points to beginning of the array   

/*
The second line of the above code points to the object of const type, which means you cannot * * use pd pointer * * to change the value of the object pointed to by pd:
*/
*pd = 29.89;      // Not allow
pd[2] = 222.22;   // Not allow
rates[0] = 99.99; // Yes, because the rates array is not const type


//In the above example, whether using pointer notation (* pointer) or array notation (pointer[i]),
//You cannot use the pointer pd to change the value of the pointed content. However, rates is not declared / / const,
//So you can still use rates to change the value. Of course, you can point pd to another location:

pd++;       /*Make pd point to rates[1], allowing*/   

/*When a pointer to a constant is used as a parameter of a function, it indicates that the function does not intend to change data with a pointer. For example,
function*/void show_array(const double ar[], int n);/*Can be declared in this form*/: void show_array(const double *ar, int n);   
/*
You can assign the address of non constant data or constant data to a pointer to a constant:
*/
double rates[5] = {88.99, 100.12, 59.45, 183.11, 340.5};
const double locked[4] = {0.08, 0.075, 0.0725, 0.07};
const double * pc = rates;    // legitimate
pc = locked;                  // legitimate
pc = &rates[3];               //legitimate  

/*However, only the addresses of non constant data can be assigned to ordinary pointers:*/
double rates[5] = {88.99, 100.12, 59.45, 183.11, 340.5};
const double locked[4] = {0.08, 0.075, 0.0725, 0.07};
double * pnc = rates;          // legitimate
pnc = locked;                  // Wrongful
pnc = &rates[3];               // legitimate  

/*The show array() function can receive ordinary arrays or constant groups as arguments, because both addresses can be assigned to pointers to constants:*/
show_array(rates, 5);    // legitimate
show_array(locked, 4);   // legitimate   

/*
Therefore, using const when a function is declared can not only protect the data, but also allow the function to receive groups of constants. Like the following function
mult_array()Unable to receive address for constant group:
*/
mult_array(rates, 5, 1.2);    // legitimate
mult_array(locked, 4, 1.2);   // Wrongful  

Show array() prototype is void show array (const double ar [], int n);
Or void show array (const double *, int);
The prototype of the mult'array() function is void mult'array (double ar [], int n, double mult);
The function prototype can be referred to Array parameter Article

The following is more about the usage of const. For example, when declaring and initializing a pointer, the pointer can only point to one place. The way of declaration is a bit dramatic, but it is different from the position of const in the above example:
double rates[5] = {88.99, 100.12, 59.45, 183.11, 340.5};
double * const pc = rates; // pc points to the first address of the array
pc = & rates [2]; / / illegal, pc cannot point to other places
*pc = 92.99; / / legal. The value of pc cannot be changed, but the value of the object pointed to can be changed
Such a pointer can still change the value of the object it points to, but it can only point to the position it was originally given.
If you want the pointer value and the pointer value to remain the same, you can use two const s:

double rates[5] = {88.99, 100.12, 59.45, 183.11, 340.5};
const double * const pc = rates;`
pc = &rates[2];               // Not allow
*pc = 92.99;                  // Not allow  

Posted by Trek15 on Mon, 30 Dec 2019 23:03:56 -0800