Re: [問題] 雙重指標

作者: loveme00835 (髮箍)   2020-06-15 23:52:48
※ 引述《spong (請輸入ID)》之銘言:
: #include<iostream>
: using namespace std;
: int main(void)
: {
: int x = 5;
: int *ptr=&x;
: int **temp =&ptr;
: cout << "&ptr="<<ptr<< endl;
: cout << "*ptr=" << *ptr<< endl;
: cout << "&temp=" <<temp <<endl;
: cout << "**temp="<<**temp <<endl;
: return 0;
: }
: 既然雙重指標,是指標的指標,為什麼不能宣告int *temp 去指向 int *ptr呢?
: 一定要用**temp?
千萬不要把指標的值用來理解指標怎麼運作, 因為在沒有搭配型別
的情況是沒有意義的, 用不合法的操作取得的指標也是不能用的.
陣列和類別一樣對於 sub-object (子物件) 的位址關係是有明確定
義的, 對於大小為 N 的陣列 A 來說, 假如索引 0 <= i, j < N,
而且 i < j, 那麼 &A[i] < &A[j]. ([expr.add]/4.2). 陣列名稱
單獨做為敘述使用的話具備型別和它宣告的時候一樣, 但當你對它
用 subscript 運算子的時候, 你會先得到轉型之後的指標, 才會得
到指標運算的結果, 此指標指向陣列的第一個元素 ([conv.array]/1)
. A[i] 等同於 *(A + i), 都是指標運算的結果 ([dcl.array]/9).
只是你要確定這個 i 必須是合法的陣列索引 (0 <= i < N). 另外
單獨的物件本身被視為是大小為 1 的陣列 ([expr.add]/4.2).
有了以上遊戲規則, 就可以說明下面操作全是不合法的:
int a[10];
*(a + 10) = 0; // error
int j, i;
if ((&i + 1) == &j) {
*(&i + 1) = 0; // error
}
即使指標値相同, 但是因為來源不同, 算出來的位址也會有合法與
否的問題. 所以我們必須從型別出發, 才能理解每個操作真正的涵
義, 而不是轉成 void* 印出來看就行.
假設我們有一個陣列, 陣列元素型別為 T, 陣列大小為 N, 那我們
一般宣告陣列的時候會是用下面的方式:
T array[N];
我們可以用下面的工具搭配 decltype 運算子來檢視每一個敘述的
型別為何:
int array[10];
template <typename>
struct print_type;
print_type< decltype(array) >(); // compilation error
上面這行會編譯錯誤, 不過是預期的結果, 現階段在 C++ 我們要看
到完整的型別還只能透過這樣的方式, 編譯器會把型別放在錯誤訊
息裡:
error: invalid use of incomplete type 'struct print_type<int [10]>
從上面我們就可以看到 array 的型別是 int[10], 獲取陣列元素型
別的方法會比較醜一點, 記得引入 <type_traits> 標頭檔:
#include <type_traits>
print_type< std::remove_extent<decltype(array)>::type >();
std::remove_extent 可以用來取得元素的型別, 另外還有一個工具
是 std::extent 可以讓我們知道陣列有幾個元素, 兩者搭配就可以
更深入了解一個陣列型別:
template <std::size_t>
struct print_size;
int array2[3][4];
print_size<
std::extent<decltype(array2)>::value >(); // 3
print_type<
std::remove_extent<decltype(array2)>::type >(); // int[4]
從上面的結果我們可以看到 array2 是一個大小為 3 的 int[4] 陣
列. 沒錯, 在 C++ 裡陣列是允許巢狀的, 嚴格說起來目前只有陣列
的陣列, 還沒有多維陣列的概念.
接著我們就可以用以上的背景知識來檢驗你上一篇的程式碼:
int ar[3][4] = {{2,4,6,8},{1,3,5,7},{10,11,12,13}};
ar // type = int[3][4], size = 3
ar+3 // type = int(*)[4], 3 exceeds the size of ar
*(ar+3) // type = int(&)[4], same as 'ar[3]' but we cannot
// de-ref an element which is not in array.
*(ar+3)+4 // type = int*, same as 'ar[3] + 4', meaningless
*(*(ar+3)+4) // type = int&, same as 'ar[3][4]', meaningless
有的指標値已經不合法了, 所以即使它能印出來觀察也是沒有意義
的. (對陣列取値會得到元素的左値參考, 所以會多出 & 符號, 和
std::remove_extent 的結果只有差在這裡) 同理本篇的程式碼也有
對應的工具可以用: std::remove_pointer. 假如我們宣告一個指標
如下:
T* ptr;
ptr 只能用來指向型別為 T 的物件 (位址)
T obj;
ptr = &obj;
給定一個指標型別, 我們就可以知道它應該指向何種物件:
int* pi;
print_type<
std::remove_pointer<decltype(pi)>::type >(); // int
int** ppi;
print_type<
std::remove_pointer<decltype(ppi)>::type >(); // int*
Reference
N4850 http://eel.is/c++draft/
作者: Lipraxde (Lipraxde)   2020-06-16 20:57:00
自從不能用 ycm 後我還在想怎麼看一堆 auto 的程式,沒想到 decltype 可以拿來用,C++ 總是令我驚奇
作者: shadow0326 (非議)   2020-06-20 23:03:00
推 學到一招了
作者: lc85301 (pomelocandy)   2020-06-24 23:39:00
這招感覺跟 rust 的 let () = var 有點像

Links booklink

Contact Us: admin [ a t ] ucptt.com