C's Level two pointer

Source: Internet
Author: User

----"The essence of C language is the flexibility of pointers. The goal of learning pointers is to use pointers as little as possible. "

A problem was encountered while knocking binary search tree (binary lookup trees). At the time of deletion, if the root node is deleted, the address of the root node should be changed, and the implementation will be very painful. It turns out that a good way to do this is to use level two pointers, which makes it easy to change the address of the root node. I just need to change the stored address in the pointer to the root node. In order to understand the level two pointer, do a little experiment:

typedefstructtree_node{intVal; Tree_node*left, *right, *parent; Tree_node (intx) {val=x; Left=NULL; Right=NULL; Parent=NULL; }}node,*Pnode;intMain () {Node x (1); cout<<&x<<Endl; Node*p = &x; cout<<&p<<"    "<<p<<"    "<<p->val<<endl;//&p:p own address P: point to (x) P: Point at Address (x) Val valueNode **pp = &p; cout<<&pp<<"   "<<pp<<"   "<< (*PP) <<"   "<< (*PP)->val<<endl;//&pp:pp own address pp: point to (P) address *pp: Remove the value at the address where pp is stored (p) (&x)//you can see that Pnode is actually a node*, so here Pnode is a (node**) is a two-level pointer to P (one-level pointer)Pnode *pnode = &p; cout<<&pnode<<"   "<<pnode<<"   "<< (*pnode) <<"   "<< (*pnode)->val<<Endl; return 0; }    

Finally, some basic operations of searching binary tree are completed. Attached Gitlab warehouse Link: [Email protected]:luntai/algorithm.git

C's Level two pointer

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.