如何:声明和使用内部指针及托管数组 (C++/CLI)

下面 C++/CLI 示例演示如何声明和使用内部指向数组。

重要

此语言功能支持 /clr 编译器选项,但是,不会 /ZW 编译器选项。

示例

3t2sye8x.collapse_all(zh-cn,VS.110).gif代码

// interior_ptr_arrays.cpp
// compile with: /clr
#define SIZE 10

int main() {
   // declare the array
   array<int>^ arr = gcnew array<int>(SIZE);

   // initialize the array
   for (int i = 0 ; i < SIZE ; i++)
      arr[i] = i + 1;

   // create an interior pointer into the array
   interior_ptr<int> ipi = &arr[0];

   System::Console::WriteLine("1st element in arr holds: {0}", arr[0]);
   System::Console::WriteLine("ipi points to memory address whose value is: {0}", *ipi);

   ipi++;
   System::Console::WriteLine("after incrementing ipi, it points to memory address whose value is: {0}", *ipi);
}

3t2sye8x.collapse_all(zh-cn,VS.110).gifOutput

1st element in arr holds: 1
ipi points to memory address whose value is: 1
after incrementing ipi, it points to memory address whose value is: 2

请参见

参考

interior_ptr (C++/CLI)