指针作为函数参量

指针作为函数参量,以地址的方式传递数据,可以用来返回函数处理结果;实参是数组名时形参可以是指针。

题目:读入三个浮点数,将整数部分和小数部分分别输出

#include <iostream>

using namespace std;

void  splitfloat(float x, int *intpart,  float *fracpart)

{  //形参intpart fracpart是指针

   *intpart=int(x); // x的整数部分

   *fracpart=x-*intpart; //x的小数部分

}

int main()

{

         int i, n;

         float x, f;    

         cout<<"Enter three(3) floating point numbers"
    << endl;

         for (i = 0; i < 3; i++)

         {

           cin >> x;

           splitfloat(x,&n,&f); //变量地址做实参

           cout<<"Integer Part is "<< n
      <<"   Fraction Part is "<<f<<endl;

         }

}

运行结果:

 

Enter three(3) floating point numbers

4.7

Integer Part is 4 Fraction Part is 0.7

8.913

Integer Part is 8 Fraction Part is 0.913

-4.7518

Integer Part is -4 Fraction Part is -0.7518

输出数组元素的内容和地址

#include <iostream>

#include <iomanip>

using namespace std;

void Array_Ptr(long *P, int n)

{        int i;

         cout<<"In func, address of array is "
    <<unsigned long(P)<<endl;

         cout<<"Accessing array using pointers"<< endl;

         for (i = 0; i < n; i++)

         { cout<<"   Address for index "<<i<<" is "
      <<unsigned long(P+i);

           cout<<"  Value is "<<*(P+i)<<endl;

         }

}

int main()

{        long list[5]={50, 60, 70, 80, 90};

                   cout<<"In main, address of array is "

      << unsigned long(list)<<endl;

         cout<<endl;

                 Array_Ptr(list,5);

}

某一次运行结果:

In main, address of array is 6684132

In func, address of array is 6684132

Accessing array using pointers

   Address for index 0 is 6684132  Value is 50

   Address for index 1 is 6684136  Value is 60

   Address for index 2 is 6684140  Value is 70

   Address for index 3 is 6684144  Value is 80

   Address for index 4 is 6684148  Value is 90

用指向常量的指针做形参

#include<iostream>

using namespace std;

const int N=6;

void print(const int *p,int n);

int main()

{  int array[N];

    for(int i=0;i<N;i++)

         cin>>array[i];

    print(array,N);

}

void print(const int *p, int n)

{

     cout<<"{"<<*p;

     for(int i=1;i<n;i++)

         cout<<"."<<*(p+i);

     cout<<"}"<<endl;

}

当函数的返回值是地址时,该函数就是指针型函数。声明:存储类型 数据类型 *函数名();

指向函数的指针  存储类型 数据类型 *函数名)();数据指针指向数据存储区,而函数指针指向的程序代码的存储区(函数名是函数代码的恰是地址)。

#include <iostream>

using namespace std;

void print_stuff(float data_to_ignore);

void print_message(float list_this_data);

void print_float(float data_to_print);

void (*function_pointer)(float);       //void类型的函数指针

 

int main()   //主函数

{

         float pi = (float)3.14159;

         float two_pi = (float)2.0 * pi;

         print_stuff(pi);

         function_pointer = print_stuff;        //函数指针指向print_stuff

      function_pointer(pi);    //函数指针调用,相当于调用print_stuff

      function_pointer = print_message; //函数指针指向print_message

      function_pointer(two_pi);      //函数指针调用

      function_pointer(13.0);          //函数指针调用

      function_pointer = print_float;       //函数指针指向print_float

      function_pointer(pi);    //函数指针调用

      print_float(pi);

}

void print_stuff(float data_to_ignore)

{        cout<<"This is the print stuff function.\n";    }

void print_message(float list_this_data)

{        cout<<"The data to be listed is "<<list_this_data<<endl;    }

void print_float(float data_to_print)

{        cout<<"The data to be printed is "<<data_to_print<<endl;    }

//运行结果:

 This is the print stuff function.

 This is the print stuff function.

 The data to be listed is 6.283180

 The data to be listed is 13.000000

 The data to be printed is 3.141590

 The data to be printed is 3.141590

最新文章

  1. 彻底理解webservice SOAP WSDL
  2. python之初级学习
  3. Android开发--adb,SQLite数据库运用
  4. iOS遍历程序内某个文件夹下所有文件的属性
  5. CLR Profiler
  6. twitter storm 源码走读之5 -- worker进程内部消息传递处理和数据结构分析
  7. python 练习 1
  8. HDU 4658 Integer Partition(整数拆分)
  9. 彻底卸载 RAD Studio 2009/2010/XE+ 的步骤
  10. 基于Greenplum Hadoop分布式平台的大数据解决方案及商业应用案例剖析
  11. select默认选中项颜色为灰色,选择后变为黑色(js实现)
  12. 【Java SE】如何安装JDK以及配置Java运行环境
  13. 工作笔记--自动切换host的python code
  14. php 解决file_put_contents 不生效
  15. IDEA+Maven+各个分支代码进行合并
  16. Jvisualvm 添加插件
  17. 基于jQuery图片遮罩滑动文字切换特效
  18. BZOJ4589 Hard Nim(快速沃尔什变换FWT)
  19. 怎样在 Ubuntu 上使用 ZFS 文件系统 | Linux 中国
  20. STL_算法_05_集合算法

热门文章

  1. CDQ分治笔记+例题
  2. LOJ #2877. 「JOISC 2014 Day2」交朋友 并查集+BFS
  3. Oracle查询如何才能行转列?-sunziren
  4. ubuntu18.04误删apt-get命令恢复总结
  5. Mac 下如何快速重启 Dock 栏?
  6. stm32f103vct6外扩sram芯片
  7. nvm Nodejs 版本管理器 安装及配置
  8. function_use
  9. 洛谷P1936 水晶灯火灵 P1775 古代人的难题_NOI导刊2010提高(02)【重题请做P1936】
  10. XMind快捷键汇总