C++——指针3
指针作为函数参量
指针作为函数参量,以地址的方式传递数据,可以用来返回函数处理结果;实参是数组名时形参可以是指针。
题目:读入三个浮点数,将整数部分和小数部分分别输出
#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
C++——指针3的更多相关文章
- TODO:Golang指针使用注意事项
TODO:Golang指针使用注意事项 先来看简单的例子1: 输出: 1 1 例子2: 输出: 1 3 例子1是使用值传递,Add方法不会做任何改变:例子2是使用指针传递,会改变地址,从而改变地址. ...
- enote笔记法使用范例(2)——指针(1)智能指针
要知道什么是智能指针,首先了解什么称为 “资源分配即初始化” what RAII:RAII—Resource Acquisition Is Initialization,即“资源分配即初始化” 在&l ...
- C++虚函数和函数指针一起使用
C++虚函数和函数指针一起使用,写起来有点麻烦. 下面贴出一份示例代码,可作参考.(需要支持C++11编译) #include <stdio.h> #include <list> ...
- C++11 shared_ptr 智能指针 的使用,避免内存泄露
多线程程序经常会遇到在某个线程A创建了一个对象,这个对象需要在线程B使用, 在没有shared_ptr时,因为线程A,B结束时间不确定,即在A或B线程先释放这个对象都有可能造成另一个线程崩溃, 所以为 ...
- c 数组与指针的使用注意事项
数组变量和指针变量有一点小小的区别 所以把数组指针赋值给指针变量的时候千万要小心 加入把数组赋值给指针变量,指针变量只会包含数组的地址信息 而对数组的长度一无所知 相当于指针丢失了一部分信息,我们把这 ...
- Marshal.Copy将指针拷贝给数组
lpStatuss是一个UNITSTATUS*的指针类型实例,并包含SensorDust字段 //定义一个数组类型 byte[] SensorDust = new byte[30] //将指针类型拷贝 ...
- C++智能指针
引用计数技术及智能指针的简单实现 基础对象类 class Point { public: Point(int xVal = 0, int yVal = 0) : x(xVal), y(yVal) { ...
- EC笔记:第三部分:17、使用独立的语句将newed对象放入智能指针
一般的智能指针都是通过一个普通指针来初始化,所以很容易写出以下的代码: #include <iostream> using namespace std; int func1(){ //返回 ...
- 智能指针shared_ptr的用法
为了解决C++内存泄漏的问题,C++11引入了智能指针(Smart Pointer). 智能指针的原理是,接受一个申请好的内存地址,构造一个保存在栈上的智能指针对象,当程序退出栈的作用域范围后,由于栈 ...
- 智能指针unique_ptr的用法
unique_ptr是独占型的智能指针,它不允许其他的智能指针共享其内部的指针,不允许通过赋值将一个unique_ptr赋值给另一个unique_ptr,如下面错误用法: std::unique_pt ...
随机推荐
- OpenLayers动态测量距离和面积,并可自定义测量的线样式
全局操作变量 /** * @description 标注弹出框 */ HtmlPopup = null; /** * @description 临时图层类数据源 */ VectorSource = n ...
- Android中点击按钮获取星级评分条的评分
场景 效果 注: 博客: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 获取编程相关电子书.教程推送与免费下载. 实现 将布局改为Lin ...
- Apache Solr JMX服务远程代码执行漏洞复现
0x00 漏洞介绍 该漏洞源于默认配置文件solr.in.sh中的ENABLE_REMOTE_JMX_OPTS配置选项存在安全风险. Apache Solr的8.1.1和8.2.0版本的自带配置文件s ...
- SpringCloud之eureka注册中心入门
eureka注册中心 一.基本概念 SpringCloud封装 了Netflix公司的eureka作为自己微服务的注册中心.这个注册中心和dubbo中的zookeeper很相似,简单来说,只要你可以将 ...
- spring cloud微服务快速教程之(十) gateway 服务网关
0.前言 gateway是spring的二代网关, 作为Netflix Zuul的替代者,是异步非阻塞网关 ,ZUUL2也是异步非阻塞的,但未纳入spring cloud整合计划 基于WebFlux ...
- Jupyter Notebook 常用快捷键 (转)
Jupyter Notebook 有两种键盘输入模式. 编辑模式,允许你往单元中键入代码或文本:这时的单元框线是绿色的. 命令模式,键盘输入运行程序命令:这时的单元框线为蓝色. 命令模式 (按键 Es ...
- int16、int32、int64的范围
做了一个 项目本地测了没问题发布到正式环境上,几天之后有个统计页面报错了,看了本地是正常的, 于是就排查,发现 ID 列 在对 字符串转int 时候 由于用了 Convert.TonInt16 长度不 ...
- 吴裕雄--天生自然 R数据分析:2014年美国人时间使用调查(ATUS)饮食与健康模块文件分析
# libraries we'll need library(car) # for avplots library(tidyverse) # for general utility functions ...
- 关于iScroll在安卓移动端/chrome模拟移动端上下滑动卡顿问题处理!!!!真实可靠!!!已解决!!!
滑动卡顿效果 安卓手机打开微信浏览网页,Chrome模拟手机浏览网页,都出现的问题滑动卡顿! 修改代码点: 1. <style type="text/css"> ...
- Android之碎片Fragment
Fragment是个特别的存在,有点像报纸上的专栏,看起来只占据页面的一小块,但是这一小块有自己的生命周期,可以自行其是,仿佛独立王国,并且这一小块的特性无论在哪个页面,给一个位置就行,添加以后不影响 ...