一,盡量用引用來(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
}