函式參數為指標

Prerequisites:

指標參數

傳指標是把原始變數的記憶體位址傳給呼叫的函式,傳指標的好處是函式可以存取原始變數。

函式的參數是指標,代表參數是位址。指標參數語法為資料型態* 參數名

void func1(int* param1) {
}

取值運算子,取出指標參數的內容。

當函式的參數為指標,指標就是位址,代表可以透過取值運算子*,取出位址存放的內容。

*param1
1
2
3
void func1(int* param1) {
  cout << "param1=" << *param1 << endl;
}
執行結果
param1=10

修改指標(位址)的內容

使用*指標 = 修改內容,修改指標的內容。

*param1 = 20;

完整程式碼

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//函式的參數是指標,代表參數是位址
void func1(int* param1) {
  //透過取值運算子*,取出位址存放的內容
  cout << "param1=" << *param1 << endl;
  //使用`*指標 = 修改內容`,修改指標的內容
  *param1 = 20;
}
int main() {
  int var1 = 10;
  cout << "修改前 var1 =" << var1 << endl;
  //使用取位址運算子&,取出var1變數的記憶體開始位址,將位址作為引數Argument傳進函式fun1()
  func1(&var1);
  cout << "修改後 var1 =" << var1 << endl;
  return 0;
}
執行結果
修改前 var1 =10
param1=10
修改後 var1 =20

引數為指標變數

上一個程式是把位址&var1傳進函式,下面的程式碼是先將位址&var放入int*指標變數,再把指標變數傳入函式。

1
2
3
4
5
6
7
8
9
void func1(int* param1) {
  cout << "param1=" << *param1 << endl;
}
int main() {
  int var1 = 10;
  int* p = &var1;
  func1(p);
  return 0;
}
執行結果
param1=10

作為函式傳回值

如果有一個功能要求函式返回多個值,只有一個函式傳回值根本不夠用,可以把參數作為傳回值。

以下程式是在一群學生數學/英文/歷史成績中,分別找出各科數學/英文/歷史最大的分數。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
using namespace std;
//宣告Student類別
class Student {
public:
  //Student建構式,將參數設給私有成員name, mathScore, engScore, historyScore
  Student(string name, double mathScore, double engScore, double historyScore)
  :name(name),mathScore(mathScore),engScore(engScore),historyScore(historyScore){}

  //取出name, mathScore, engScore, historyScore公有方法
  string getName(){
    return name;
  }
  double getMathScore(){
    return mathScore;
  }
  double getEngScore(){
    return engScore;
  }
  double getHistoryScore(){
    return historyScore;
  }
private:
  //私有成員屬性
  string name;
  double mathScore;
  double engScore;
  double historyScore;
};

//宣告函式,參數為Student陣列,陣列大小,maxMax指標,engMax指標,historyMax指標
void getMax(Student studentArr[], int arrSize, double* mathMax, double* engMax, double* historyMax){
  for(int i = 0; i < arrSize; i++) {
    //找出數學 英文 歷史最大的分數
    if(studentArr[i].getMathScore() > *mathMax)
      *mathMax = studentArr[i].getMathScore();
    if(studentArr[i].getEngScore() > *engMax)
      *engMax = studentArr[i].getEngScore();
    if(studentArr[i].getHistoryScore() > *historyMax)
      *historyMax = studentArr[i].getHistoryScore();
  }
}

int main() {
  //建立3個學生物件,利用名字,數學英文歷史成績建立物件
  Student studentArr[] =
  {Student("Mary", 50, 30, 100),
   Student("Bill", 70, 80, 90),
   Student("Lisa", 20, 99, 10)};

  //宣告三個存最大數學 英文 歷史成績的指標
  double* mathMax = new double(0);
  double* engMax = new double(0);
  double* historyMax = new double(0);

  //將Student陣列,陣列大小,最大數學 英文 歷史成績的指標傳入getMax函式
  compare(studentArr, 3, mathMax, engMax, historyMax);

  //印出各科最大分數
  cout << "mathMax = " << *mathMax << endl;
  cout << "mathEng = " << *engMax << endl;
  cout << "mathHistory = " << *historyMax << endl;
  // release momery
  delete mathMax;
  mathMax = nullptr;
  delete engMax;
  engMax = nullptr;
  delete historyMax;
  historyMax = nullptr;
  return 0;
}
執行結果
mathMax = 70
mathEng = 99
mathHistory = 100

減少記憶體空間的使用

每呼叫一次函式,就會為函式參數分配記憶體空間,使用指標作為參數,指標占記憶體空間固定8 byte,若參數為char[100]則會占100 byte的記憶體空間,相比之下指標可以節省更多記憶體空間。

results matching ""

    No results matching ""