Level 2 pointer ** P

Source: Internet
Author: User

First, understand several concepts:

1. Reference a common variable to obtain a level-1 pointer. For example, if int A = 0; int * P = & A, & A is the first-level pointer. Because the value of & A is the address of a and the value of P is also the address of a, then & A and P are the first-level pointer variables (simple as pointers ), unreference & A, int B = * & A; this B is equal to 0.

2. when a common variable is passed as a form parameter to a function, passing the value of the parameter means simply copying the value of the variable to the temporary variable and passing the temporary variable to the function, however, the temporary variable has no relationship with the original variable, so the function cannot change the value of the external original variable.

3. when a level-1 pointer variable is passed to the function as a form parameter, although the value of this level-1 pointer (that is, the address pointing to the object) is copied to the temporary variable, however, the content of this temporary variable is an address. By referencing an address, you can modify the value of the memory unit pointed to by this address. Conclusion: When a level-1 pointer is passed as a parameter, the value of an external variable can be changed, that is, the content pointed to by the level-1 pointer, but the pointer itself cannot be changed.


For level 2 pointers:

1. A second-level pointer is obtained through unreferencing. For a first-level pointer to obtain the original variable, int ** P, * P is a pointer, ** P is the value of the original variable.

2. the first-level pointer and second-level pointer both point to a memory unit. The first-level pointer points to a memory unit that stores the source variable value. The second-level pointer points to a memory unit that stores the first-level pointer address.

        int a =1;int *b =&a;int **c=&b;cout<<&a<<endl;cout<<b<<endl;        cout<<*c<<endl;        /*以上输出都是a的地址,而下面两行就是b的地址*/cout<<&b<<endl;cout<<c<<endl;
3. Second-level pointers are generally used when the external pointer of the function needs to be modified. Because the pointer variable outside the function can only obtain the address of the external pointer variable in the memory unit through the second-level pointer resolution reference, modify the content pointed to by this address.

#include <unistd.h>#include <stdio.h>#include <stdlib.h>void increase(int** ptr) {      **ptr = **ptr + 1;/*对于一个二级指针进行解引用得到一级指针,对于一个一级指针解引用得到原始变量*/      *ptr = NULL;  }   int main(int argc, char** argv) {    int count = 7;      int* countPtr = &count;    increase(&countPtr);/*当二级指针(&countPtr)传进来时,这个二级指针的值(就是一级指针变量的地址)会copy一份到临时变量传给函数形参,然而对这个临时变量操作就会影响一级指针变量countptr,从而影响到二级指针所指向的原变量*/    printf("countPtr = %p\n", countPtr);    return 0;}
/*这段代码,运行结果count = 8, countPtr = NULL;*/
或者是
void GetMemory( char **p, int num ){    *p = (char *) malloc( num );}void Test( void ){    char *str = NULL;    GetMemory( &str, 100 );    strcpy( str, "hello" );    printf( str ); }


 

4. parameter passing in C language is a value transfer. when passing a pointer to a function, it is actually a value transfer, unless the double pointer is used.

Summary

First, the pointer variable, which is also a variable, also occupies memory space in the memory unit. The first-level pointer variable points to the value of a common variable, and the second-level pointer variable points to the address of the first-level pointer variable.

This article references: http://www.fenesky.com/blog/2014/07/03/pointers-to-pointers.html

Level 2 pointer ** P

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.