Summary of function parameters, pointer parameters, and const Modification

Source: Internet
Author: User

 

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

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.