[C++] 指標

Posted on Mon, Mar 22, 2021 C++
實作日期:20210322

How to resolve the error: "invalid conversion from int to int*" 不能直接指定位置(0除外)

指標的宣告

int *p = 0;

int n = 10;
int *p2 = &n; //&取得該變數的位置

&是提領運算子,可以取得變數的記憶體位置。

計算陣列長度的方式

int arr[10] = {0};
int *p = arr

cout<<end(arr) - begin(arr)<<endl;
cout<<sizeof(arr)/sizeof(*arr)<<endl;
cout<<*(&arr + 1) -arr<<endl; //難以理解

手動配置記憶體空間

int *p new int;
int p2 new int(100);
int p3 new int[10]{0,1,2,3};

可以做到和[[vector]]一樣的動態陣列,但直接用[[vector]]比較快。

配置二維陣列還沒看

參考資料:https://openhome.cc/Gossip/CppGossip/