C/C++语言参数传递----值传递、引用传递、指针传递、指针引用传递
来源:互联网 发布:如何变漂亮 知乎 编辑:程序博客网 时间:2024/04/29 02:47
1.值传递
- void f( int p){
- printf("\n%x",&p);
- printf("\n%x",p);
- p=0xff;
- }
- void main()
- {
- int a=0x10;
- printf("\n%x",&a);
- printf("\n%x\n",a);
- f(a);
- printf("\n%x\n",a);
- }
通过上例我们可以看到,int a=0x10,存放的地址为0x12ff44,值为10,当调用f(a)时,传递给p的值为10,但是p的地址为0x12fef4,当改变p=0xff,时是改变地址为0x12fef4中的内容,并没有改变0x12ff44中的内容,所以调用f(a),后a的值仍然为0x10,所以值传递无法改变变量的值。示意图如下:
2.引用传递
- void f( int & p){
- printf("\n%x",&p);
- printf("\n%x",p);
- p=0xff;
- }
- void main()
- {
- int a=0x10;
- printf("\n%x",&a);
- printf("\n%x\n",a);
- f(a);
- printf("\n%x\n",a);
- }
通过上面引用传递传递案例我们可以看到,调用f(a)时,传递给p的是a的地址,所以p和a的地址都是0X12ff44,所以p就是a,改变p当然能改变a。示意图如下:
3.指针传递
- void f( int*p){
- printf("\n%x",&p);
- printf("\n%x",p);
- printf("\n%x\n",*p);
- *p=0xff;
- }
- void main()
- {
- int a=0x10;
- printf("\n%x",&a);
- printf("\n%x\n",a);
- f(&a);
- printf("\n%x\n",a);
- }
通过指针传递的案例我们可以看到,调用f(&a)是将a的地址0x12ff44传递给p,则*p就指向了a的内容,改变*p后,a的内容自然就改变了,示意图如下:
4.指针的引用传递
- void f( int*&p){
- printf("\n%x",&p);
- printf("\n%x",p);
- printf("\n%x\n",*p);
- *p=0xff;
- }
- void main()
- {
- int a=0x10;
- printf("\n%x",&a);
- printf("\n%x\n",a);
- int *b=&a;
- printf("\n%x",&b);
- printf("\n%x",b);
- printf("\n%x\n",*b);
- f(b);
- printf("\n%x\n",a);
- }
为了使用指针的引用传递我们要新建一个指针b,然后将b的引用传递给p,其实p就是b的一个拷贝,*p=*b都指向a,所以改变*p的内容也就改变a的内容。示意图如下:
我们再来看一下如果不用指针的引用传递会出现什么结果
- void f( int*p){
- printf("\n%x",&p);
- printf("\n%x",p);
- printf("\n%x\n",*p);
- *p=0xff;
- }
- void main()
- {
- int a=0x10;
- printf("\n%x",&a);
- printf("\n%x\n",a);
- int *b=&a;
- printf("\n%x",&b);
- printf("\n%x",b);
- printf("\n%x\n",*b);
- f(b);
- printf("\n%x\n",a);
- printf("\n%x\n",b);
- }
从结果中我们可以看到调用f(b)时,传递给p的是b的内容,但是&b,和&p是不一样的,虽然*p和*b都指向a。示意图如下:
5.错误案例
- #include <stdio.h>
- #include <malloc.h>
- #include <string.h>
- void Allocate(char* p,int size){
- printf("\n%x",&p);
- printf("\n%x",p);
- p=(char*)malloc(size);
- }
- void Free(char* p){
- free(p);
- }
- void main()
- {
- char *str=NULL;
- printf("\n%X",&str);
- printf("\n%X",str);
- Allocate(str,100);
- strcpy(str,"Hello World!");
- printf("\n%s",str);
- Free(str);
- printf("\nstr=%s",str);
- }
当执行strcpy(str,"Hello World!"),时会报Unhandled exception in CPoint.exe:0xC0000005:Access Violation,这是因为我们参用的是指针传递,从运行结果我们可以看到str的地址为0x12ff44,当调用Allocate(str,100)时,传递给p的是str,的内容也就是0,所以p为0,但是&p并不是和&str一样的,所以在运行p=(char*)malloc(size)时,是给0x12fef0分配的100个字节,并没有给0x12ff44分配字节,所以*str还是空。所以会报错。
5.正确案例
- #include <stdio.h>
- #include <malloc.h>
- #include <string.h>
- void Allocate(char*& p,int size){
- printf("\n%x",&p);
- printf("\n%x",p);
- p=(char*)malloc(size);
- }
- void Free(char* p){
- free(p);
- }
- void main()
- {
- char *str=NULL;
- printf("\n%X",&str);
- printf("\n%X",str);
- Allocate(str,100);
- strcpy(str,"Hello World!");
- printf("\n%s",str);
- Free(str);
- }
因为指针引用传递的是指针的拷贝,所以&str和&p,是地址是一样的,所以对p分配内容空间也就是对str分配空间,所以没有问题!
0 0
- C/C++语言参数传递----值传递、引用传递、指针传递、指针引用传递
- C/C++语言参数传递----值传递、引用传递、指针传递、指针引用传递
- C/C++语言参数传递----值传递、引用传递、指针传递、指针引用传递
- [C]值传递、指针传递、引用传递
- 【c/c++】值传递、指针传递、引用传递
- C或C++中函数的参数传递包括:值传递、指针传递、引用传递
- C或C++中函数的参数传递——值传递、指针传递、引用传递
- C/C++值传递、指针传递和引用传递详解
- C++ 参数传递方式:值传递,引用传递,指针传递
- 指针传递 引用传递
- 指针传递,引用传递
- C++:指针传递、引用传递的区别
- c++ 值传递 引用传递 指针传递
- 什么是值传递、引用传递、指针传递
- 值传递、指针传递和引用传递
- 值传递 引用传递 指针传递
- 值传递,引用传递,指针传递
- 值传递,指针传递,引用传递
- mybatis分页插件
- macOS
- Boost::circular_buffer——循环缓冲区
- mac intellij save credentials
- Swift CollectionViewCell重排
- C/C++语言参数传递----值传递、引用传递、指针传递、指针引用传递
- CentOS-6.5在线安装docker-1.7教程(升级内核)
- AtomicInteger简介
- 安装R packages报错
- 剑指offer 复杂链表的复制
- 对于REST中无状态(stateless)的一点认识
- java内建注解及源码浅析
- binary-tree-postorder-traversal
- Android AMS原理分析(1)