C Language Pointer usage details (iii) Double pointer

Source: Internet
Author: User

Two-weight pointer Example 1:
    • Question
    int **ptr1 = NULL;    cout<< "Situation one   ptr1 = =" <<ptr1<<endl;    cout<< "Situation one  *ptr1 = =" <<*ptr1<<endl;    cout<< "situation one **ptr1 = =" <<**ptr1<<endl;   Output: 0  illegal illegal
    • Reason:
1) Three-province pointers: The type of pointer ptr1 is int * *, to the type int*, specified as empty 2) *PTR1 is the point that ptr points to, that is, the null point, illegal 3) **PTR1 is the pointing point that ptr points to, that is, pointing to an empty point, illegal Example 2:
    • Question
    int * P1 = (int *) malloc (sizeof (int));    int **ptr2 = &p1;    cout<< "Situation two   ptr2 = =" <<ptr2<<endl;    cout<< "Situation two  *ptr2 = =" <<*ptr2<<endl;    cout<< "situation two **ptr2 = =" <<**ptr2<<endl;
    • Practice:
#include <bits/stdc++.h>using namespace Std;int main () {    int * p1 = (int *) malloc (sizeof (int));    int **ptr2 = &p1;    cout<< "Situation two     P1 = =" <<p1<<endl;    cout<< "Situation two    *p1 = =" <<*p1<<endl;    cout<< "Situation two    &p1 = =" <<&p1<<endl;    cout<< "Situation two   ptr2 = =" <<ptr2<<endl;    cout<< "Situation two  *ptr2 = =" <<*ptr2<<endl;    cout<< "situation two **ptr2 = =" <<**PTR2<<ENDL;}

 

    • Reason:

1) Three-province pointer: the type of pointer p is int *, which points to the type int, initialized to not specify its point 2) Three-province pointers: The type of pointer ptr2 is int * *, the point of type int*, the address of the pointer P (note that at this point ptr2 and &p type are int**, you can assign values) 3) *PTR2 is the ptr2 point, that is, &p1 point, for the P1 4) **PTR2 is the point of the ptr2 point, that is, *ptr2 Point, for *p1 5) The pointer is the address, so as &p1 points to P1 Example 3:
    • Question
   int * P2 = (int *) malloc (sizeof (int));    int num = 6;    P2 = #    int **PTR3 = &p2;
    • Practice:
#include <bits/stdc++.h>using namespace Std;int main () {    int * p2 = (int *) malloc (sizeof (int));    int num = 6;    cout<< "situation three     P2 = =" <<p2<<endl;    P2 = #    int **PTR3 = &p2;    cout<< "situation three     P2 = =" <<p2<<endl;    cout<< "situation three    *p2 = =" <<*p2<<endl;    cout<< "situation three    &p2 = =" <<&p2<<endl;    cout<< "situation three   &num = =" <<&num<<endl;    cout<< "situation three   PTR3 = =" <<ptr3<<endl;    cout<< "situation three  *PTR3 = =" <<*ptr3<<endl;    

  

    • Reason:
1) Three-province pointers: The type of the pointer p2 is int *, the type of int, the initialization is not pointed, and the pointer is &num (note that at this point P2 and &num type are int*, you can assign a value) 2) Three-province pointers: The type of pointer ptr3 is int * *, the point of type int*, the address of the pointer P2 (note that at this point ptr2 and &p2 type are int**, you can assign a value) 3) *PTR3 is the PTR3 point, that is, &p2 point, for the P2 4) **PTR3 is the point of the Ptr3 point, that is, *ptr3 Point, for *p2 5) The pointer is the address, so as &num points to num, as &p2 points to P2Spit Spit Spit Spit Spit : Write blog is tired ah ah ah ah, blog Park Why so bad use, is I open the way wrong????

C Language Pointer usage details (iii) Double 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.