建设视频网站流量,电商怎么做营销推广天气预报,郴州网站建设网络推广渠道,建设公司网站计入哪个科目C中将指针传递给函数
指针是一种将内存空间传递给函数的有效方式#xff0c;其中可包含函数完成其工作所需的数据#xff0c;也可包含操作结果。将指针作为函数参数时#xff0c;确保函数只能修改您希望它修改的参数很重要。例如#xff0c;如果函数根据以指针方式传入的半…C中将指针传递给函数
指针是一种将内存空间传递给函数的有效方式其中可包含函数完成其工作所需的数据也可包含操作结果。将指针作为函数参数时确保函数只能修改您希望它修改的参数很重要。例如如果函数根据以指针方式传入的半径计算圆的面积就不应允许它修改半径。为控制函数可修改哪些参数以及不能修改哪些参数可使用关键字 const如以下示例程序所示
#include iostream
using namespace std;void CalcArea(const double* const ptrPi, // const pointer to const dataconst double* const ptrRadius, // i.e. no changes alloweddouble* const ptrArea) // can change data pointed to
{// check pointers for validity before using!if (ptrPi ptrRadius ptrArea) *ptrArea (*ptrPi) * (*ptrRadius) * (*ptrRadius);
}int main()
{const double Pi 3.1416;cout Enter radius of circle: ;double radius 0;cin radius;double area 0;CalcArea (Pi, radius, area);cout Area is area endl;return 0;
}输出
Enter radius of circle: 10.5
Area is 346.361分析
第 35 行演示了两种 const 指针 ptrRadius 和 ptrPi 被声明为“指向 const 数据的 const 指针”因此不能修改指针包含的地址也不能修改它指向的数据。 ptrArea 显然是用于存储输出的参数因为不能修改该指针的值地址但可修改它指向的数据。第 8 行在使用函数的指针参数前检查其有效性。在调用者不小心将这三个参数之一设置为 NULL 指针时您不希望函数计算面积因为这种非法访问将导致应用程序崩溃。
无可否认引用变量比指针更容易处理因为引用变量隐藏了所有的解引用和间接引用机制。但是程序员仍然应该学会使用指针作为函数参数因为有一些任务特别是在处理 C 字符串时最好使用指针完成、另外C库中还有许多使用指针作为形参的函数。
以下是使用指针形参的函数的定义
void doubleValue(int *val)
{*val * 2;
}这个函数的目的是使 val 指向的变量翻倍。当 val 被解引用时* 运算符对 val 指向的变量起作用。该语句可以将地址存储在 val 中的原始变量乘以 2。当然当调用该函数时必须使用被翻倍的变量地址作为实参而不是变量本身作为实参。
以下是一个调用 doubleValue 函数的示例
doubleValue(number);该语句使用了地址运算符将 number 的地址传递到 val 形参中。函数执行后number 的内容将被乘以 2。下面的程序演示了该函数的用法
//This program uses two functions that accept addresses of variables as arguments.
#include iostream
using namespace std;
//Function prototypes
void getNumber(int *);
void doubleValue(int *);int main()
{int number;//Call getNumber and pass the address of numbergetNumber(number);// Call doubleValue and pass the address of numberdoubleValue(number);// Display the value in numbercout That value doubled is number endl;return 0;
}
void getNumber(int *input)
{cout Enter an integer number: ;cin *input;
}void doubleValue(int *val)
{*val * 2;
}程序输出结果
Enter an integer number: 10
That value doubled is 20此程序有两个使用指针作为参数的函数。请看以下函数原型
void getNumber(int *);
void doubleValue(int *);每一个函数原型都使用符号 int * 来表示该形参是一个指向 int 的指针。与所有其他类型的形参一样不需要在原型中指定变量的名称但星号*则是必需。
getNumber 函数要求用户输入一个整数值。以下 cin 语句可以将用户输入的值存储在内存中
cin *input;间接运算符会使用户输入的值存储在 input 指向的变量中而不是 input 中。
在上面的语句中使用间接运算符是非常重要的。没有它则 cin 会将用户输入的值存储在 input 中就像该值是一个内存地址一样。如果发生这种情况则 input 将不再指向 main 函数中的 number 变量。如此一来对该指针input的后续使用即使不会产生灾难性的结果也必然出现错误。
当调用 getNumber 函数时函数 main 中 number 变量的地址作为实参传递。该函数执行后用户输入的数值将被存储在 number 中。接下来调用 doubleValue 函数同样是将 number 的地址作为实参传递这使得 number 被乘以 2。
指针变量也可以用来接收数组地址作为实参此后无论是下标还是指针符号都可以用来处理数组的内容。下面的程序演示了这一点
//This program demonstrates that a pointer may be used as a parameter to accept the address of an array. Either subscript or pointer notation may be used.
#include iostream
#include iomanip
using namespace std;
// Function prototypes
void getSales(double *sales, int size);
double totalSales(double *sales, int size);int main()
{const int QUARTERS 4;double sales[QUARTERS];getSales(sales, QUARTERS);cout setprecision(2);cout fixed showpoint;cout The total sales for the year are $;cout totalSales(sales, QUARTERS) endl;return 0;
}
void getSales(double *array, int size)
{for (int count 0; count size; count){cout Enter the sales figure for quarter ;cout (count 1) : ;cin array[count];}
}double totalSales(double *array, int size)
{double sum 0.0;for (int count 0; count size; count){sum *array;array;}return sum;
}程序输出结果
Enter the sales figure for quarter 1: 10263.98
Enter the sales figure for quarter 2: 12369.69
Enter the sales figure for quarter 3: 11542.13
Enter the sales figure for quarter 4: 14792.06
The total sales for the year are $48967.86请注意该程序的 getSales 函数中即使把形参 array 定义为一个指针其下标符号仍然可以在 cin 语句中使用
cin array[count];在 totalSales 函数中array 还可以与以下语句中的间接运算符一起使用
sum *array;而在接下来的语句中array 中的地址则可以递增以使指向下一个元素
array;上面介绍的两个语句也可以合并成以下语句
sum *array;* 运算符将首先解引用 array然后 运算符将使得 array 中的地址递增。
该文章会更新欢迎大家批评指正。
推荐一个零声学院的C服务器开发课程个人觉得老师讲得不错 分享给大家LinuxNginxZeroMQMySQLRedis fastdfsMongoDBZK流媒体CDNP2PK8SDocker TCP/IP协程DPDK等技术内容 点击立即学习C/C后台高级服务器课程 文章转载自: http://www.morning.prgdy.cn.gov.cn.prgdy.cn http://www.morning.jtqxs.cn.gov.cn.jtqxs.cn http://www.morning.yrlfy.cn.gov.cn.yrlfy.cn http://www.morning.rklgm.cn.gov.cn.rklgm.cn http://www.morning.cwyfs.cn.gov.cn.cwyfs.cn http://www.morning.crrmg.cn.gov.cn.crrmg.cn http://www.morning.wkgyz.cn.gov.cn.wkgyz.cn http://www.morning.yxnfd.cn.gov.cn.yxnfd.cn http://www.morning.lsbjj.cn.gov.cn.lsbjj.cn http://www.morning.mgkcz.cn.gov.cn.mgkcz.cn http://www.morning.cypln.cn.gov.cn.cypln.cn http://www.morning.qkpzq.cn.gov.cn.qkpzq.cn http://www.morning.mkyxp.cn.gov.cn.mkyxp.cn http://www.morning.kgphd.cn.gov.cn.kgphd.cn http://www.morning.rqkzh.cn.gov.cn.rqkzh.cn http://www.morning.qrcsb.cn.gov.cn.qrcsb.cn http://www.morning.yzygj.cn.gov.cn.yzygj.cn http://www.morning.rnlx.cn.gov.cn.rnlx.cn http://www.morning.kllzy.com.gov.cn.kllzy.com http://www.morning.gtbjc.cn.gov.cn.gtbjc.cn http://www.morning.xfwnk.cn.gov.cn.xfwnk.cn http://www.morning.xwqxz.cn.gov.cn.xwqxz.cn http://www.morning.hrzhg.cn.gov.cn.hrzhg.cn http://www.morning.njftk.cn.gov.cn.njftk.cn http://www.morning.llqch.cn.gov.cn.llqch.cn http://www.morning.dhckp.cn.gov.cn.dhckp.cn http://www.morning.mynbc.cn.gov.cn.mynbc.cn http://www.morning.lcplz.cn.gov.cn.lcplz.cn http://www.morning.thzgd.cn.gov.cn.thzgd.cn http://www.morning.xwbld.cn.gov.cn.xwbld.cn http://www.morning.btqqh.cn.gov.cn.btqqh.cn http://www.morning.cnxpm.cn.gov.cn.cnxpm.cn http://www.morning.rjmd.cn.gov.cn.rjmd.cn http://www.morning.jfsbs.cn.gov.cn.jfsbs.cn http://www.morning.xylxm.cn.gov.cn.xylxm.cn http://www.morning.wgbsm.cn.gov.cn.wgbsm.cn http://www.morning.ktntj.cn.gov.cn.ktntj.cn http://www.morning.tbnpn.cn.gov.cn.tbnpn.cn http://www.morning.qrnbs.cn.gov.cn.qrnbs.cn http://www.morning.dpgdj.cn.gov.cn.dpgdj.cn http://www.morning.qphdp.cn.gov.cn.qphdp.cn http://www.morning.srgwr.cn.gov.cn.srgwr.cn http://www.morning.wkqrp.cn.gov.cn.wkqrp.cn http://www.morning.drywd.cn.gov.cn.drywd.cn http://www.morning.gqryh.cn.gov.cn.gqryh.cn http://www.morning.twgzq.cn.gov.cn.twgzq.cn http://www.morning.ltdrz.cn.gov.cn.ltdrz.cn http://www.morning.zcyxq.cn.gov.cn.zcyxq.cn http://www.morning.psdbf.cn.gov.cn.psdbf.cn http://www.morning.pabxcp.com.gov.cn.pabxcp.com http://www.morning.nwjd.cn.gov.cn.nwjd.cn http://www.morning.ktrzt.cn.gov.cn.ktrzt.cn http://www.morning.nrbqf.cn.gov.cn.nrbqf.cn http://www.morning.qfqld.cn.gov.cn.qfqld.cn http://www.morning.hhrpy.cn.gov.cn.hhrpy.cn http://www.morning.sjpbh.cn.gov.cn.sjpbh.cn http://www.morning.sjwqr.cn.gov.cn.sjwqr.cn http://www.morning.kgxyd.cn.gov.cn.kgxyd.cn http://www.morning.wmfr.cn.gov.cn.wmfr.cn http://www.morning.nbdtdjk.cn.gov.cn.nbdtdjk.cn http://www.morning.nktgj.cn.gov.cn.nktgj.cn http://www.morning.lnwdh.cn.gov.cn.lnwdh.cn http://www.morning.gzxnj.cn.gov.cn.gzxnj.cn http://www.morning.bqrd.cn.gov.cn.bqrd.cn http://www.morning.mcfjq.cn.gov.cn.mcfjq.cn http://www.morning.xbnkm.cn.gov.cn.xbnkm.cn http://www.morning.zxwqt.cn.gov.cn.zxwqt.cn http://www.morning.pbwcq.cn.gov.cn.pbwcq.cn http://www.morning.gbpanel.com.gov.cn.gbpanel.com http://www.morning.sthp.cn.gov.cn.sthp.cn http://www.morning.sxcwc.cn.gov.cn.sxcwc.cn http://www.morning.nfdty.cn.gov.cn.nfdty.cn http://www.morning.nkdmd.cn.gov.cn.nkdmd.cn http://www.morning.ggnrt.cn.gov.cn.ggnrt.cn http://www.morning.lwwnq.cn.gov.cn.lwwnq.cn http://www.morning.xjqhh.cn.gov.cn.xjqhh.cn http://www.morning.ishoufeipin.cn.gov.cn.ishoufeipin.cn http://www.morning.sqgsx.cn.gov.cn.sqgsx.cn http://www.morning.rgrz.cn.gov.cn.rgrz.cn http://www.morning.cpktd.cn.gov.cn.cpktd.cn