0

In c++ is possible to get a slice of multidimensional array, like

  double parallelopipedPts[8][3];
  parallelopipedPts[0];
  parallelopipedPts[1];
  ...
  parallelopipedPts[7];

In c# I am trying to emulate this and tried

  double[][] parallelopipedPts = new double[8][];
  parallelopipedPts[0];
  parallelopipedPts[1];
  ...
  parallelopipedPts[7];

However I am getting memory access issues

How to solve this?

4
  • Can you expand a bit? What element are you hoping to return? Or are you trying to return the address of the element? Commented Dec 10, 2013 at 1:10
  • I would like to get a slice (one dimension) of jagged array, How to do that in c#? Commented Dec 10, 2013 at 1:13
  • You can't. See here: stackoverflow.com/a/4802062/1517578 Commented Dec 10, 2013 at 1:15
  • On that question OP states he uses a Rectangular array, however if one uses a jagged array, would it be faster as stated in an answer from that question? Commented Dec 10, 2013 at 1:17

1 Answer 1

1

In your C# code, you've created an array of 8 'double arrays'.

The double arrays in that array-of-8 have not yet been assigned, so they are null.

You can populate it with arrays-of-3-doubles (for example) like this:

for (int i = 0; i < parallelopipedPts.Length; i++)
{
    parallelopipedPts[i] = new double[3];
}

Then you can safely use your sub-arrays.

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

3 Comments

Nope, double[][] parallelopipedPts = new double[8][3]; would not compile.
Is a for loop the only way out?
I guess to be fair I've never bothered trying to make an array like this in C#, since I'd rather build a specialized class with a bit more information attached.

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.