Void testn (int n)
{
N = 0; // correct, but the external N value has not changed. Be careful about logical errors.
}
Void testn (const int N)
{
N = 0; // compilation error. Adding const can prevent logical errors.
}
Void testn (int * n)
{
N = new int (3); // correct, but the N value of the external pointer has not changed. Be careful about logical errors.
* N = 0; // correct. The value of N pointing to the variable is changed.
}
Void testn (int * const N)
{
N = new int (3); // compilation error. N is a constant pointer. Adding const can prevent logical errors.
* N = 0; // correct. The value of N pointing to the variable is changed.
}
Void testn (const int * n)
{
N = new int (3); // correct, but the N value of the external pointer has not changed. Be careful about logical errors.
* N = 0; // compilation error. Adding const can prevent the variable value pointed to by the pointer from being changed.
}
Void testn (INT ** PPN)
{
* PPN = new int [3]; // correct. The pointer value pointed to by PPN is changed.
** PPN = 6; // correct. The final variable value pointed to by PPN is changed.
* PPN [0] = 7; // correct. The final variable value pointed to by PPN is changed.
PPN = new int * [3]; // correct, but the external second-level pointer PPN value has not changed. Be careful about logical errors.
PPN [0] = new int (5); // correct, but the external level-2 pointer PPN value has not changed. Be careful about logical errors.
}
Void testn (INT ** const PPN)
{
* PPN = new int [3]; // correct. The PPN value of the second-level pointer is changed.
** PPN = 6; // correct. The final variable value pointed to by PPN is changed.
* PPN [0] = 7; // correct. The final variable value pointed to by PPN is changed.
// PPN [0] = new int (5); // correct, same as * PPN
// PPN = new int * [3]; // compilation error. PPN is a constant second-level pointer and const to prevent logical errors.
}
Void testn (int * const * PPN)
{
// * PPN = new int [3]; // compilation error. The PPN value is a constant pointer and cannot be changed * PPN Value
** PPN = 6; // correct. The final variable value pointed to by the second-level pointer is changed.
* PPN [0] = 7; // correct. The final variable value pointed to by the second-level pointer is changed.
PPN = new int * [3]; // correct, but the external second-level pointer PPN value has not changed. Be careful about logical errors.
PPN [0] = new int (5); // compilation error. The value of * PPN cannot be changed.
}
Void testn (INT const ** PPN) // same as testn (const int ** PPN)
{
* PPN = new int [3]; // correct. The pointer value pointed to by the second-level pointer PPN is changed.
// ** PPN = 6; // compilation error. The value of ** PPN cannot be changed.
// * PPN [0] = 7; // compilation error, * PPN [0] = ** PPN
// PPN = new int * [3]; // compilation error. The conversion from 'int ** 'to 'const int **' is incorrect.
}
// Conclusion: When the function parameter is a value passing method, adding const can prevent logical errors.
// When the function parameter is the address transfer method, adding const can prevent the parameter data from being changed.
// When the parameter is a multi-level pointer, you can view the const modifier object and *
// When there is no * between the const and the variable, the multi-level pointer itself is modified.
// When there is a * between the const and the variable, it modifies the multi-level pointer value.
// This formula is calculated until the final variable value is modified, so the effect of const before and after the variable type is the same