zoukankan      html  css  js  c++  java
  • c++ 指针(三)

    指针

    (6)传递指针给函数

    只需要简单地声明函数参数为指针类型即可

    #include <iostream>
    #include <ctime>
     
    using namespace std;
    void getSeconds(unsigned long *par);
     
    int main ()
    {
       unsigned long sec;
     
     
       getSeconds( &sec );
     
       // 输出实际值
       cout << "Number of seconds :" << sec << endl;
     
       return 0;
    }
     
    void getSeconds(unsigned long *par)
    {
       // 获取当前的秒数
       *par = time( NULL );
       return;
    }

    结果:Number of seconds :1294450468

    能接受指针作为参数的函数,也能接受数组作为参数

    #include <iostream>
    using namespace std;
     
    // 函数声明
    double getAverage(int *arr, int size);
     
    int main ()
    {
       // 带有 5 个元素的整型数组
       int balance[5] = {1000, 2, 3, 17, 50};
       double avg;
     
       // 传递一个指向数组的指针作为参数
       avg = getAverage( balance, 5 ) ;
     
       // 输出返回值
       cout << "Average value is: " << avg << endl; 
        
       return 0;
    }
     
    double getAverage(int *arr, int size)
    {
      int    i, sum = 0;       
      double avg;          
     
      for (i = 0; i < size; ++i)
      {
        sum += arr[i];
       }
     
      avg = double(sum) / size;
     
      return avg;
    }

    结果:Average value is: 214.4

    传递指针给函数的过程补充:

    #include <iostream>
    #include <ctime>
    
    void getSeconds(unsigned long *par);
    
    int main()
    {
        unsigned long sec = 0;
    
        cout << "Value of sec = :" << sec << endl;
        cout << "Value of &sec = :" << &sec << endl;
        cout << endl;
        getSeconds(&sec);
    
        // 输出实际值
        cout << "Number of seconds :" << sec << endl;
    
        return 0;
    }
    
    void getSeconds(unsigned long *par)
    {
        cout << "Value of *par = :" << *par << endl;
        cout << "Value of par = :" << par << endl;
        cout << "Value of &par = :" << &par << endl;
        cout << endl;
        // 获取当前的秒数
        *par = time(NULL);
        cout << "Value of *par = :" << *par << endl;
        cout << endl;
        return;
    }

    (7)从函数返回指针

    必须声明一个返回指针的函数

    int * myFunction()
    {
    ....
    }

    C++ 不支持在函数外返回局部变量的地址,除非定义局部变量为 static 变量

    下面的函数,它会生成 10 个随机数,并使用表示指针的数组名(即第一个数组元素的地址)来返回它们:

    #include <iostream>
    #include <ctime>
    #include <cstdlib>
     
    using namespace std;
     
    // 要生成和返回随机数的函数
    int * getRandom( )
    {
      static int  r[10];
     
      // 设置种子
      srand( (unsigned)time( NULL ) );
      for (int i = 0; i < 10; ++i)
      {
        r[i] = rand();
        cout << r[i] << endl;
      }
     
      return r;
    }
     
    // 要调用上面定义函数的主函数
    int main ()
    {
       // 一个指向整数的指针
       int *p;
     
       p = getRandom();
       for ( int i = 0; i < 10; i++ )
       {
           cout << "*(p + " << i << ") : ";
           cout << *(p + i) << endl;
       }
     
       return 0;
    }

    结果:

    624723190
    1468735695
    807113585
    976495677
    613357504
    1377296355
    1530315259
    1778906708
    1820354158
    667126415
    *(p + 0) : 624723190
    *(p + 1) : 1468735695
    *(p + 2) : 807113585
    *(p + 3) : 976495677
    *(p + 4) : 613357504
    *(p + 5) : 1377296355
    *(p + 6) : 1530315259
    *(p + 7) : 1778906708
    *(p + 8) : 1820354158
    *(p + 9) : 667126415

    补充:

    1.指针的本质是变量,可以是各种数据类型,定义一个指针 "*ip",其中 "ip" 需要赋于一个地址(可以用 & 符号获取其他变量的地址再赋值给 ip),而 "*ip" 是一个具体的值,即读取地址后获得的值; 

    #include <iostream>
    using namespace std;
    
    int main()
    {
        int var = 20;
        int *ip;
        ip = &var;
    
        cout << "var的值:";
        cout << var << endl;
    
        cout << "变量 ip 的储存地址:";
        cout << ip << endl;
    
        cout << "指针 *ip 的值:";
        cout << *ip << endl; 
        return 0;
    }

    结果:

    var的值:20
    变量 ip 的储存地址:0x7fff5e7deae8
    指针 *ip 的值:20

    2.

    C++ 中常量引用、指向常量的指针、常量指针的区别

     3.

     4.

    5. 关于值传递,指针传递,引用传递

    值传递:

    形参是实参的拷贝,改变形参的值并不会影响外部实参的值。从被调用函数的角度来说,值传递是单向的(实参->形参),参数的值只能传入,不能传出。当函数内部需要修改参数,并且不希望这个改变影响调用者时,采用值传递。

    指针传递:

    形参为指向实参地址的指针,当对形参的指向操作时,就相当于对实参本身进行的操作

    引用传递:

    形参相当于是实参的"别名",对形参的操作其实就是对实参的操作,在引用传递过程中,被调函数的形式参数虽然也作为局部变量在栈中开辟了内存空间,但是这时存放的是由主调函数放进来的实参变量的地址。被调函数对形参的任何操作都被处理成间接寻址,即通过栈中存放的地址访问主调函数中的实参变量。正因为如此,被调函数对形参做的任何操作都影响了主调函数中的实参变量。

    #include<iostream>
    using namespace std;
    //值传递 4  void change1(int n){
        cout<<"值传递--函数操作地址"<<&n<<endl;         //显示的是拷贝的地址而不是源地址  6   n++;
     }
     
     //引用传递10 void change2(int & n){
         cout<<"引用传递--函数操作地址"<<&n<<endl; 
         n++;
     }
      //指针传递15 void change3(int *n){
          cout<<"指针传递--函数操作地址 "<<n<<endl; 
         *n=*n+1;
      } 
     int     main(){
         int n=10;
         cout<<"实参的地址"<<&n<<endl;
         change1(n);
         cout<<"after change1() n="<<n<<endl;
         change2(n);
         cout<<"after change2() n="<<n<<endl;
         change3(&n);
         cout<<"after change3() n="<<n<<endl;
         return true;
     }

     6.

    C++ 函数指针 & 类成员函数指针

    7.

     8.指针分配与释放一个空间

  • 相关阅读:
    leetcode_357. Count Numbers with Unique Digits
    leetcode_712. Minimum ASCII Delete Sum for Two Strings
    leetcode_865. Smallest Subtree with all the Deepest Nodes
    leetcode_919. Complete Binary Tree Inserter
    leetcode_1014. Capacity To Ship Packages Within D Days
    leetcode_998. Maximum Binary Tree II
    leetcode_654. Maximum Binary Tree
    leetcode_894. All Possible Full Binary Trees
    leetcode_486. Predict the Winner
    二叉树的遍历
  • 原文地址:https://www.cnblogs.com/expedition/p/11347826.html
Copyright © 2011-2022 走看看