07 引用 指標 和傳遞
一,儘量用引用來傳遞變數
二,儘量用const 來限制形參的修改
程式碼區 |
---|
資料區 |
----------------- |
常量區 |
---------------- |
堆 |
---------------- |
棧 |
--------------- |
三,全域性變數和靜態變數都是預設初始化0.
|----------------|
程式碼區 |
---|
資料區 |
----------------- |
常量區 |
---------------- |
堆 |
---------------- |
棧 |
--------------- |
int main(int argc, char* argv[])
{
int m = 15,n=25; //c
vor(m); //形參的傳入,相當於是m的複製一份給vor函式,用完消失
cout << m <<endl;
inr(m); //數值地址傳入,是對地址進行修改。(引用和取別名)是對源引數的再次命名。。而指標是對指標指向進行交換。
cout << m <<endl;
return 0;
}
void inr(int& m) //---------------引用
{
++m;
}
void vor(int m)
{
++m;
}
//--------------------------------------------------------------------
//--------------------------------------------------------------------
//-------------------------------------------------------------------
//===============函式指標====================
// 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;//函式指標 將函式名改為(fp),並實現初始化
//void sort(int a[], int n);將函式名改為上面的函式指標。
int x[5];
fp = output; //將指標函式 等於 output函式
output(x,5); //-------------
fp(x,5); //---------兩者輸出結果一樣
//--------------------
fp = reset; //必須是引數相同的同類函式
fp(x,5);
fp = output; //將指標函式 等於 output函式
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) //初始化地址,將陣列全部歸零
{
memset(a,0,sizeof(int) *n); //a為陣列首地址 三個引數
//for(int i =0; i < n; i++)
// a[i] = 0
}
相關文章
- 關於指標傳遞和指標的指標指標
- 值傳遞和引用傳遞
- C++中引用傳遞與指標傳遞區別(進一步整理)C++指標
- JavaScript的值傳遞和引用傳遞JavaScript
- Java的值傳遞和引用傳遞Java
- GO切片傳值/引用/指標Go指標
- 1.0 - 指標和引用指標
- Java中的值傳遞和引用傳遞Java
- C++引用和指標C++指標
- nim的引用和指標指標
- 《指標和引用小結》指標
- 細說引用和指標指標
- 值傳遞與引用傳遞
- 關於值傳遞和引用傳遞的解釋
- Qt 訊號槽傳遞指標QT指標
- [C++]指標和引用(一)C++指標
- [C++]指標和引用(二)C++指標
- [C++]指標和引用(三)C++指標
- [C++]指標和引用(四)C++指標
- 指標和引用的區別指標
- Java 從陣列來看值傳遞和引用傳遞Java陣列
- js中函式引數值傳遞和引用傳遞JS函式
- C#程式設計引用型別和值型別 以及引用傳遞和值傳遞C#程式設計型別
- 詳解c++指標的指標和指標的引用C++指標
- 指標與引用指標
- 快速搞懂值傳遞與引用傳遞
- Go高階特性 13 | 引數傳遞:值、引用及指標之間的區別?Go指標
- 傳遞指標引數(函式內部給指標賦值)示例指標函式賦值
- Java - 是值傳遞還是引用傳遞Java
- js函式引數值傳遞和引用傳遞簡單介紹JS函式
- 這一次,徹底解決Java的值傳遞和引用傳遞Java
- java的引用:用C++/C的引用和指標去理解JavaC++指標
- 解惑4:java是值傳遞還是引用傳遞Java
- golang工作筆記(二)值傳遞與引用傳遞Golang筆記
- chan中傳遞map資料,傳遞的是引用
- java方法中只有值傳遞,沒有引用傳遞Java
- JS是按值傳遞還是按引用傳遞?JS
- GO中的函式設計時候,引數傳遞選擇傳遞值還是傳遞指標?Go函式指標