07 引用 指針 和傳遞

一,盡量用引用來(lái)傳遞變量
二,盡量用const 來(lái)限制形參的修改

代碼區(qū)
數(shù)據(jù)區(qū)
-----------------
常量區(qū)
----------------
----------------
---------------

三,全局變量和靜態(tài)變量都是默認(rèn)初始化0.
|----------------|

代碼區(qū)
數(shù)據(jù)區(qū)
-----------------
常量區(qū)
----------------
----------------
---------------

int main(int argc, char* argv[])
{
int m = 15,n=25; //c
vor(m); //形參的傳入,相當(dāng)于是m的復(fù)制一份給vor函數(shù),用完消失
cout << m <<endl;
inr(m); //數(shù)值地址傳入,是對(duì)地址進(jìn)行修改。(引用和取別名)是對(duì)源參數(shù)的再次命名。。而指針是對(duì)指針指向進(jìn)行交換。
cout << m <<endl;
return 0;
}

void inr(int& m) //---------------引用
{
++m;

}

void vor(int m)
{
++m;

}
//--------------------------------------------------------------------
//--------------------------------------------------------------------
//-------------------------------------------------------------------
//===============函數(shù)指針====================

// class.cpp : Defines the entry point for the console application.
//

include "stdafx.h"

include <iostream>

include <string>

include <cstring>

using namespace std;

void reset(int a[], int n);
void input(int a[], int n);
void output(int a[], int n);
void sort(int a[], int n);

int main(int argc,int argv[])
{
void (fp)(int a[],int n) = NULL;//函數(shù)指針 將函數(shù)名改為(fp),并實(shí)現(xiàn)初始化
//void sort(int a[], int n);將函數(shù)名改為上面的函數(shù)指針。
int x[5];
fp = output; //將指針函數(shù) 等于 output函數(shù)
output(x,5); //-------------
fp(x,5); //---------兩者輸出結(jié)果一樣

//--------------------
fp = reset; //必須是參數(shù)相同的同類函數(shù)
fp(x,5);
fp = output; //將指針函數(shù) 等于 output函數(shù)
output(x,5); //-------------
return 0;
}

void output(int a[], int n)
{
for(int i= 0;i <n ;i++)
cout << a[i] << " " <<endl;
}

void reset(int a[], int n) //初始化地址,將數(shù)組全部歸零
{
memset(a,0,sizeof(int) *n); //a為數(shù)組首地址 三個(gè)參數(shù)
//for(int i =0; i < n; i++)
// a[i] = 0

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容