0

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 to variables in another array? Or is the practice to use only one pointer?

Thanks for any help, in advance

4
  • 4
    int vs double - can you clarify if that is just a typo? Also.... what is the question/problem? Commented Oct 6, 2011 at 9:44
  • Okay, I've attempted to clarify. Commented Oct 6, 2011 at 10:00
  • p = &mylist[0] - This is pointing to the first element of the array. When you have the pointer to the first element, then there shouldn't be any problem to get to the rest of the array elements. What are you trying to achieve here, are you doing P/Invoke stuff? Commented Oct 6, 2011 at 10:08
  • Yes, I'm trying to call a DLLimport and pass a array pointer reference, and stating it's size as a parameter. Does that make sense? Commented Oct 6, 2011 at 11:06

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.