运行效果: ​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型,没有返回值。