const int n=100;是用來指定陣列的size;然後第一個for loop是塞值進入陣列,第二個for迴圈是把目前的陣列值印出來.
若沒有塞值進入陣列的話,印出來的陣列值是殘存在那個記憶體位置裡的值,如下列例子:
我們也可用pointer(指標)的方式來對陣列存取,
陣列的第一個元素的位址(address)就是陣列的名稱,
因此我們可以把此名稱指定給poiner然後循序遞增或遞減pointer來存取陣列,
如下範例:
int* ptr =a;:把此名稱指定給poiner
ptr++;:循序遞增或遞減pointer來存取陣列
Subsequently, let's introduce the Dynamic Array.
The differences between static and dynamic are the declaration and memory handling.
For declaration, a "new" reserved word needs to be along with the variable.And we also need to delete the pointer variable to release the reserved memory block. It is a very important thing to prevent from a memory leak.
Following is the example code, you can focus on the colored words.
沒有留言:
張貼留言