You can define a pointer to an array... but only within a limited area by
using the fixed statement
(
http://msdn.microsoft.com/library/de...vclrffixed.asp) ... in this case you would do it like this:
int []array = new int[100];
fixed(int* pointer = &array[0])
{
//use the pointer
}
By using the fixed keyword, you are telling the CLR that you want to force
it not to move the data that the pointer is pointing at around in memory
(which is a real risk with managed code).
Be sure to note that the pointer is only usable within with in the statement
(ie between { and }) immediately following the fixed keyword, so you cannot
take this pointer with you... however it is still usable for things like
P/Invokes and some pointer arithmetic.
Brendan
"Peter Demeyer" wrote:
I can't manage to define a simple pointer to an array.
The following C++ code doesn't work in C# (compiler error: "You can
only take the address of unfixed expression inside of a fixed statement
initializer")
Int32 *pointer;
int []array = new int[100];
pointer = &array[0];
Can anyone please instruct me how to do this properly in C#?
Thanks,
Peter