×
嵌入式 > 技术百科 > 详情

C++堆栈、参数的传递与指针

发布时间:2020-05-29 发布时间:
|
//一。指针函数

#include "stdafx.h"

 
void fun1(int a,int b)
{
printf("%d %d ",a,b);
}
 
 
int _tmain(int argc, _TCHAR* argv[])
{
 
void (*fun)(int x,int y);//void 是被指函数的返回值类型,int为被指函数的形参类型
fun=fun1;
fun(10,20);
return 0;
}

二。参数的传递

// 0224.cpp : 定义控制台应用程序的入口点。
//
 
#include "stdafx.h"
int a=3;
int b=4;
void fun(int &x,int &y)//这种情况是引用传递。即没有在栈里开辟新的空间,交换了x,y的内存数据
{//注意这儿&的意义不是取地址
int tem;
tem=x;
x=y;
y=tem;
}
void fun1(int x,int y)//这种情况时值传递,会开在栈里辟两个空间x,y,会交换栈里的值而不会作用于堆
{
int tem;
tem=x;
x=y;
y=tem;
}
 
void fun2(int *p1,int *p2)
{
int tem;
tem=*p1;
*p1=*p2;
*p2=tem;
}
int _tmain(int argc, _TCHAR* argv[])
{
fun(a,b);
printf("a=%d  b=%d ",a,b);
 
fun1(a,b);
printf("a=%d  b=%d ",a,b);
 
fun2(&a,&b);//形参是指针实参为地址
printf("a=%d  b=%d ",a,b);
 
return 0;
}
 

『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
Keil5(MDK5)在调试(debug)过程中遇到的问题