Detailed description of the pointer and reference of the c ++ pointer
It shows the pointer passed to the method by using pointer and pointer reference, so as to better use it. (The pointer here is not a two-dimensional array.) Why do we need to use them? When we pass a pointer as a parameter to a method, we actually pass a copy of the pointer to the method, it can also be said that the pass pointer is the value transfer of the pointer. If we modify the pointer inside the method, a problem occurs. Modifying the pointer in the method only changes the copy of the pointer rather than the pointer itself. The original pointer retains the original value. The following code describes the problem: copy the code int m_value = 1; void func (int * p) {p = & m_value;} int main (int argc, char * argv []) {int n = 2; int * pn = & n; cout <* pn <endl; func (pn ); cout <* pn <endl; return 0 ;} copy the code. The output result is two pointers with pointers. the pointer is used as the parameter to copy the code void func (int ** p) {* p = & m_value; // You can also allocate the memory * p = new int; ** p = 5;} int main (int argc, char * argv []) based on your needs. {int n = 2; int * pn = & n; cout <* pn <endl; func (& p N); cout <* pn <endl; return 0;} copy the code. Let's take a look at the func (int ** p) Method p: a pointer, here we will not modify it, otherwise we will lose the pointer address pointed to by this pointer * p: It is the pointer pointed to, it is an address. If we modify it, the content of the pointer to be modified. In other words, we modified the * pn pointer ** p in the main () method: the two unreferences point to main () in the * pn content pointer reference method, let's look at the reference code of the pointer and copy the code int m_value = 1; void func (int * & p) {p = & m_value; // You can also allocate the memory p = new int; * p = 5;} int main (int argc, char * argv []) {int n = 2; int * pn = & n; cout <* pn <endl; func (pn); cout <* pn <endl; return 0 ;} copy the code to check whether the func (int * & p) Method p is a pointer reference. * pn * p in the main () method is main () method.