开发者

Create array of pointers from an array of double in C#

开发者 https://www.devze.com 2023-04-11 07:33 出处:网络
I\'m trying to create an array of pointer references to a double array.For example; double[] mylist = new double[100];

I'm trying to create an array of pointer references to a double array. For example;

double[] mylist = new double[100];
fixed (double* p = mylist) { }

Now as the MSDN documentation states, that is equivalent to p = &mylist[0] This is only taking the first value, is it possible to create an array of pointers开发者_StackOverflow社区 to variables in another array? Or is the practice to use only one pointer?

Thanks for any help, in advance


Array elements are located in contiguous memory, so it's usually suffcient to have a pointer to the first element and do pointer arithmetic to get to the others.


When you have a typed pointer to a vector (or, more accurately, the first item in a vector), it works like in C/C++; all you need is the single pointer, and you can use it either as an individual item or as a zero-based array; you can still access p[3], except now instead of using array access metaphors, this is applying "3 * the-item-size as an offset relative to p". So:

p[3] = 1.0;

is fine. Note, of course, that if you go outside the array bounds accidentally, bad things will happen.

0

精彩评论

暂无评论...
验证码 换一张
取 消