zoukankan      html  css  js  c++  java
  • 关于指针形参的一点体会


    关于指针形参的一点体会:

    现在假设指针实参为地址&a(0001)在这个&a(0001)地址空间内所存储的是一个int类型的变量为0;
    那么在被调用函数中,会临时开辟一个空间一片临时地址空间&b(0002),在&b的所指向的地址空间内,其所存储的内容为
    一个地址值,这个地址值,就是&a(0001).
    所以现在在被调函数中,有两种变化的操作:
    1. 对&b(0002)所指向的地址内存储的内容,进行改变,比如讲将&b(0002)所指向空间内的值就是(0001)改成0003,那么这个值的改变对
    &a没有任何影响。
    2. 对&b(0002),所指向的地址(0001)的所指向的内容,就是这个地址所存储的int类型的变量(原为0)进行改变,比如现在改为1,那么这将对&a的地址值,仍然是(0001),没有影响,但这改变其所指向的内容,将其改为了(int)1.


    这就是,关于指针形参时候,所说的指针的指针的意思。

    可以参考如下博客和代码:


    参考 :
    http://blog.csdn.net/richerg85/article/details/14450183
    http://www.cnblogs.com/lipeil/archive/2012/10/26/2740729.html
    和:http://blog.sina.com.cn/s/blog_6ce6d0bf01016utv.html


    关于指针形参的一点一点体会
    ```cpp
    //============================================================================
    // Name : TS.cpp
    // Author : jiudianren
    // Version :
    // Copyright : Your copyright notice
    // Description : Hello World in C++, Ansi-style
    //============================================================================

    #include <iostream>
    #include <functional>

    using namespace std;


    void swap(int *a,int *b)
    {
    int temp;
    temp=*a;
    *a=*b;
    *b=temp;
    cout<<"a=" <<a<<" ,"<<"b="<<b<<endl;
    cout<<"*a=" <<*a<<" ,"<<"*b="<<*b<<endl;
    cout<<"&a=" <<&a<<" ,"<<"&b="<<&b<<endl;
    }

    int main(){
    int x=1;
    int y=2;
    cout<<"x=" <<x<<" ,"<<"y="<<y<<endl;
    cout<<"&x=" <<&x<<" ,"<<"&y="<<&y<<endl;
    swap(&x,&y);
    cout<<"end:"<<endl;
    cout<<"x=" <<x<<" ,"<<"y="<<y<<endl;
    cout<<"&x=" <<&x<<" ,"<<"&y="<<&y<<endl;
    }

    ```
    -------------
    输出

    ```
    x=1 ,y=2
    &x=0x28ff1c ,&y=0x28ff18
    a=0x28ff1c ,b=0x28ff18
    *a=2 ,*b=1
    &a=0x28ff00 ,&b=0x28ff04
    end:
    x=2 ,y=1
    &x=0x28ff1c ,&y=0x28ff18
    ```

    #第二部分
    ```
    //============================================================================
    // Name : TS.cpp
    // Author : jiudianren
    // Version :
    // Copyright : Your copyright notice
    // Description : Hello World in C++, Ansi-style
    //============================================================================

    #include <iostream>
    #include <functional>

    using namespace std;


    int change(char* name){
    cout<<"*******CHANGE--BEFORE******"<<endl;
    cout<<"name=" <<name<<endl;
    cout<<"*name=" <<*name<<endl;
    cout<<"&name=" <<&name<<endl;
    name="alter";
    cout<<"*******CHANGE--AFTER********"<<endl;
    cout<<"name=" <<name<<endl;
    cout<<"*name=" <<*name<<endl;
    cout<<"&name=" <<&name<<endl;
    return 1;
    }
    int main()
    {
    char *str = "this is a test";

    cout<<"******MAIN--BEFORE*****"<<endl;
    cout<<"str=" <<str<<endl;
    cout<<"&str=" <<&str<<endl;
    change(str);
    cout<<"*****MAIN--AFTER*****"<<endl;
    cout<<"str=" <<str<<endl;
    cout<<"&str=" <<&str<<endl;
    return 1;
    }

    ```

    -------------
    输出

    ```
    ******MAIN--BEFORE*****
    str=this is a test
    &str=0x28ff2c
    *******CHANGE--BEFORE******
    name=this is a test
    *name=t
    &name=0x28ff10
    *******CHANGE--AFTER********
    name=alter
    *name=a
    &name=0x28ff10
    *****MAIN--AFTER*****
    str=this is a test
    &str=0x28ff2c

    ```


    # 第三部分
    ```cpp
    //============================================================================
    // Name : TS.cpp
    // Author : jiudianren
    // Version :
    // Copyright : Your copyright notice
    // Description : Hello World in C++, Ansi-style
    //============================================================================

    #include <iostream>
    #include <functional>
    #include <stdlib.h>
    using namespace std;

    #define PT 1
    void my_malloc(void* * p, int size)
    {


    #if PT
    cout<<"------func befor"<<endl;
    cout<<"p : "<<p<<endl;
    cout<<"*p: "<<*p<<endl;
    cout<<"&p: "<<&p<<endl;
    #endif

    *p=malloc(sizeof(int)* size);

    #if PT
    cout<<"---------func after"<<endl;
    cout<<"p: "<<p<<endl;
    cout<<"*p: "<<*p<<endl;
    cout<<"&p: "<<&p<<endl;
    #endif
    return ;
    }
    int main()
    {
    int temp=11;
    int *a;
    a=&temp;
    #if PT
    cout<<"---------main before"<<endl;
    cout<<"a : "<<a<<endl;
    cout<<"*a :"<<(int)*a<<endl;
    cout<<"&a :"<<&a<<endl;
    #endif


    my_malloc((void ** )&a,10);


    #if PT
    cout<<"----------main after"<<endl;
    cout<<"a: "<<a<<endl;
    cout<<"*a: "<<*a<<endl;
    cout<<"&a: "<<&a<<endl;
    #endif


    return 1;
    }
    ```

    输出
    ---------------
    ```

    ---------main before
    a : 0x28ff2c
    *a :11
    &a :0x28ff28
    ------func befor
    p : 0x28ff28
    *p: 0x28ff2c
    &p: 0x28ff10
    ---------func after
    p: 0x28ff28
    *p: 0x952ab8
    &p: 0x28ff10
    ----------main after
    a: 0x952ab8
    *a: 9776208
    &a: 0x28ff28

    ```

    # 引用

    ```

    void swapref(int &a,int &b)
    {
    cout << "******************before swapref:******************"<<endl;
    cout<<"a=" <<a<<" ,"<<"b="<<b<<endl;
    cout<<"&a=" <<&a<<" ,"<<"&b="<<&b<<endl;
    int temp;
    temp=a;
    a=b;
    b=temp;
    cout << "******************after swapref:******************"<<endl;
    cout<<"a=" <<a<<" ,"<<"b="<<b<<endl;
    cout<<"&a=" <<&a<<" ,"<<"&b="<<&b<<endl;
    }
    int main(){
    int x=1;
    int y=2;
    cout<<"******MAIN--BEFORE*****"<<endl;
    cout<<"x=" <<x<<" ,"<<"y="<<y<<endl;
    cout<<"&x=" <<&x<<" ,"<<"&y="<<&y<<endl;
    //swap(&x,&y);
    swapref(x, y);
    cout<<"*****MAIN--AFTER*****"<<endl;
    cout<<"x=" <<x<<" ,"<<"y="<<y<<endl;
    cout<<"&x=" <<&x<<" ,"<<"&y="<<&y<<endl;
    }
    ```

    ```
    输出 ,待跑
    ```

  • 相关阅读:
    修改SharePoint 2013中item Created by 信息
    用powershell批量新增user profile
    如何发一封回复的时候收件人和发件人不同的邮件
    SharePoint 2013配置 Workflow Manager
    正则表达式
    go
    HTML5
    js-example
    css3
    jquery-example
  • 原文地址:https://www.cnblogs.com/jiudianren/p/5668578.html
Copyright © 2011-2022 走看看