Condividi tramite


Procedura: dichiarare e utilizzare i puntatori interni e le matrici gestite (C++/CLI)

Il seguente esempio di codice C++/CLI mostra come è possibile dichiarare e utilizzare un puntatore interno ad un array.

Importante

Questa funzionalità del linguaggio è supportata dall'opzione del compilatore /clr, ma non dall'opzione del compilatore di /ZW.

Esempio

Codice

// 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);
}

Output

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

Vedere anche

Riferimenti

interior_ptr (C++/CLI)