VB編程:利用指針實現數組的插入-43

運行效果:
VB編程:利用指針實現數組的插入-43


程序代碼:
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

Private Sub Form_Load()
On Error GoTo 1
    Dim arr()
    arr = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
    Text1.Text = ""
    For j = 0 To UBound(arr)
        Text1.Text = Text1.Text & arr(j)
    Next j
    ReDim Preserve arr(UBound(arr) + 1)         '重定義數組arr上標
    Debug.Print UBound(arr)
    Dim i As Integer
    i = InputBox("請輸入要插入的位置:", "插入")
    CopyMemory arr(i + 1), arr(i), (UBound(arr) - i + 2) * 16  '變體變量佔16個字節, 不是很明白
    arr(i) = InputBox("要插入的數字", "插入")
    Text2.Text = ""
    For j = 0 To UBound(arr)
        Text2.Text = Text2.Text & arr(j)
    Next j
1: End Sub

學習總結:
CopyMemory() 
函數功能描述:將一塊內存的數據從一個位置複製到另一個位置 
函數原型  
VOID CopyMemory( PVOID Destination,  CONST VOID *Source,  DWORD Length  ); 
參數 
Destination  要複製內存塊的目的地址。  
Source  要複製內存塊的源地址。  
Length  指定要複製內存塊的大小,單位爲字節  
返回值  該函數爲VOID型,沒有返回值。