
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
1076
|
Chapter 19: Unsafe Code
pointers in the array are initialized as null pointers. Finally, each NewBrush structure
is added to the array. Now you have a fully initialized array of pointers. From here
you can use this array as you wish:
unsafe
{
NewBrush theNewBrush1 = new NewBrush( );
NewBrush theNewBrush2 = new NewBrush( );
NewBrush theNewBrush3 = new NewBrush( );
NewBrush*[] arrayOfNewBrushPtrs = new NewBrush*[3];
arrayOfNewBrushPtrs[0] = &theNewBrush1;
arrayOfNewBrushPtrs[1] = &theNewBrush2;
arrayOfNewBrushPtrs[2] = &theNewBrush3;
}
This array of pointers to NewBrush objects must be referenced as a pointer to a pointer
in unsafe code. The following code shows how to dereference each pointer within
the array
arrayOfNewBrushPtrs:
unsafe
{
fixed(NewBrush** ptrArrayOfNewBrushPtrs = arrayOfNewBrushPtrs)
{
for (int counter = 0; counter < 3; counter++)
{
ptrArrayOfNewBrushPtrs[counter]->BrushType = counter;
Console.WriteLine(ptrArrayOfNewBrushPtrs[counter]->BrushType);
Console.WriteLine((int)ptrArrayOfNewBrushPtrs[counter]);
}
}
}
The for loop initializes the BrushType field of each of the pointers to NewBrush objects
in the array. This field is initialized to the current value of the loop counter (
counter).
The next two lines display this newly initialized field and the address of