C++堆棧、參數的傳遞與指針
#include "stdafx.h"
本文引用地址:http://dyxdggzs.com/article/201612/324449.htmvoid fun1(int a,int b)
{
printf("%d %d",a,b);
}
int _tmain(int argc, _TCHAR* argv[])
{
void (*fun)(int x,int y);//void 是被指函數的返回值類(lèi)型,int為被指函數的形參類(lèi)型
fun=fun1;
fun(10,20);
return 0;
}
二。參數的傳遞
// 0224.cpp : 定義控制臺應用程序的入口點(diǎn)。
//
#include "stdafx.h"
int a=3;
int b=4;
void fun(int &x,int &y)//這種情況是引用傳遞。即沒(méi)有在棧里開(kāi)辟新的空間,交換了x,y的內存數據
{//注意這兒&的意義不是取地址
int tem;
tem=x;
x=y;
y=tem;
}
void fun1(int x,int y)//這種情況時(shí)值傳遞,會(huì )開(kāi)在棧里辟兩個(gè)空間x,y,會(huì )交換棧里的值而不會(huì )作用于堆
{
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);//形參是指針實(shí)參為地址
printf("a=%d b=%d",a,b);
return 0;
}
評論