call by value call by address call by

  • Slides: 43
Download presentation

call by value? call by address (或 call by pointer)? call by reference? •

call by value? call by address (或 call by pointer)? call by reference? • • • Call by value: swap(int a, int b) Call by address: swap(int* a, int* b) Call by reference: swap (int &a, int &b)

Call by value: swap(int a, int b) void swap(int a, int b){ int tmp

Call by value: swap(int a, int b) void swap(int a, int b){ int tmp = a; a = b; b = tmp; } • 呼叫swap(x, y)後,x和y的值並不會有變化。

測試一下您的理解 int g_int = 0; void change. Ptr(int* p. Int){ p. Int = &g_int;

測試一下您的理解 int g_int = 0; void change. Ptr(int* p. Int){ p. Int = &g_int; } void main(){ int local. Int = 1; int* local. PInt = &local. Int; change. Ptr(local. PInt); printf("%dn", *local. PInt); } 將印出?

int g_int = 0; void change. Ptr(int* p. Int){ p. Int = &g_int; }

int g_int = 0; void change. Ptr(int* p. Int){ p. Int = &g_int; } void main(){ int local. Int = 1; int* local. PInt = &local. Int; change. Ptr(local. PInt); printf("%dn", *local. PInt); } 1. 印出來的數字仍然會是local. Int的1 1. 因為chang. Ptr中的p. Int是由local. PInt「複製」 過去的,對p. Int做改變並不會反映到local. PInt 身上。

指標的指標(pointer to pointer ) int g_int = 0; void change. Ptr(int** p. Int){ *p.

指標的指標(pointer to pointer ) int g_int = 0; void change. Ptr(int** p. Int){ *p. Int = &g_int; } void main(){ int local. Int = 1; int* local. PInt = &local. Int; change. Ptr(&local. PInt); printf("%dn", *local. PInt); } 將印出?

pointer to pointer int g_int = 0; void change. Ptr(int** p. Int){ *p. Int

pointer to pointer int g_int = 0; void change. Ptr(int** p. Int){ *p. Int = &g_int; } void main(){ int local. Int = 1; int* local. PInt = &local. Int; change. Ptr(&local. PInt); printf("%dn", *local. PInt); } g_int 0 local. Int 1 local. PInt

pointer to pointer int g_int = 0; void change. Ptr(int** p. Int){ *p. Int

pointer to pointer int g_int = 0; void change. Ptr(int** p. Int){ *p. Int = &g_int; } void main(){ int local. Int = 1; int* local. PInt = &local. Int; change. Ptr(&local. PInt); printf("%dn", *local. PInt); } g_int 0 local. Int 1 local. PInt p. Int

pointer to pointer int g_int = 0; void change. Ptr(int** p. Int){ *p. Int

pointer to pointer int g_int = 0; void change. Ptr(int** p. Int){ *p. Int = &g_int; } void main(){ int local. Int = 1; int* local. PInt = &local. Int; change. Ptr(&local. PInt); printf("%dn", *local. PInt); } g_int 0 local. Int 1 local. PInt *p. Int

reference to pointer • 同樣的功能也可以用「指標的參考」來做 int g_int = 0; void change. Ptr(int* &ref. PInt){

reference to pointer • 同樣的功能也可以用「指標的參考」來做 int g_int = 0; void change. Ptr(int* &ref. PInt){ ref. PInt = &g_int; } void main(){ int local. Int = 1; int* local. PInt = &local. Int; change. Ptr(local. PInt); printf("%dn", *local. PInt); }

reference to pointer int g_int = 0; void change. Ptr(int* &ref. PInt){ ref. PInt

reference to pointer int g_int = 0; void change. Ptr(int* &ref. PInt){ ref. PInt = &g_int; } void main(){ int local. Int = 1; int* local. PInt = &local. Int; change. Ptr(local. PInt); printf("%dn", *local. PInt); } g_int 0 local. Int 1 local. PInt

reference to pointer int g_int = 0; void change. Ptr(int* &ref. PInt){ ref. PInt

reference to pointer int g_int = 0; void change. Ptr(int* &ref. PInt){ ref. PInt = &g_int; } void main(){ int local. Int = 1; int* local. PInt = &local. Int; change. Ptr(local. PInt); printf("%dn", *local. PInt); } g_int 0 local. Int 1 ref. PInt即local. PInt

reference to pointer int g_int = 0; void change. Ptr(int* &ref. PInt){ ref. PInt

reference to pointer int g_int = 0; void change. Ptr(int* &ref. PInt){ ref. PInt = &g_int; } void main(){ int local. Int = 1; int* local. PInt = &local. Int; change. Ptr(local. PInt); printf("%dn", *local. PInt); } g_int 0 local. Int 1 ref. PInt即local. PInt

指標陣列 • 一種常見的混淆是pointer array (指標陣列) 與pointer to pointers,因為兩種都可以寫 成**的型式。 • 如,int**可能是pointer to integer, 也可能是integer

指標陣列 • 一種常見的混淆是pointer array (指標陣列) 與pointer to pointers,因為兩種都可以寫 成**的型式。 • 如,int**可能是pointer to integer, 也可能是integer pointer array。 • pointer array的常見例子:main(int argc, char** argv)其實應該是main(int argc, char* argv[])。 argv …

函數指標的常見例子 • stdlib. h中提供的qsort函式。此函式之原型 如下: void qsort(void* base, size_t n, size_t size, int (*cmp)(const

函數指標的常見例子 • stdlib. h中提供的qsort函式。此函式之原型 如下: void qsort(void* base, size_t n, size_t size, int (*cmp)(const void*, const void*)); – 其中的int (*cmp)(const void*, const void*) 就使 用到函式指標。

如何看懂複雜的指標宣告 • 在The C++ Programming Language中有提到一 個簡單的要訣:由右向左讀!!讓我們用這個要訣再 來試一次。(用英文解說會比較清楚哦!) • const double *ptr; // ptr

如何看懂複雜的指標宣告 • 在The C++ Programming Language中有提到一 個簡單的要訣:由右向左讀!!讓我們用這個要訣再 來試一次。(用英文解說會比較清楚哦!) • const double *ptr; // ptr is a pointer points to double, which is a constant double *const ptr; // ptr is a constant pointer points to double const* ptr; // ptr is a pointer points to constant double const double *const ptr; // ptr is a constant pointer points to double, which is a constant

再來「英翻中」 • const double *ptr; // ptr is a pointer points to double, which

再來「英翻中」 • const double *ptr; // ptr is a pointer points to double, which is a constant • 中譯:ptr是一個指標,指向double的記憶 體空間,此double值是常數(不可更改)

再來「英翻中」 • double *const ptr; // ptr is a constant pointer points to double

再來「英翻中」 • double *const ptr; // ptr is a constant pointer points to double • 中譯:ptr是一個常數指標(ptr值不可更改), 指向儲存double的記憶體空間

再來「英翻中」 • double const* ptr; // ptr is a pointer points to constant double

再來「英翻中」 • double const* ptr; // ptr is a pointer points to constant double • 中譯:ptr是一個指標,指向固定double值 的記憶體空間

再來「英翻中」 • const double *const ptr; // ptr is a constant pointer points to

再來「英翻中」 • const double *const ptr; // ptr is a constant pointer points to double, which is a constant • 中譯:ptr是一個常數指標,指向double的 記憶體空間,此double值是常數(不可更改)