-1

For my C# project, I'm working with a 16x16x16 3D char array(char[,,]) and need to set the values of all elements to char '0'. Preferably during definition (char[,,] my3Dbasis = ...). Of course, I could do this manually, but such would be extremely unconventional and not suitable for my needs. Any ideas?

2

2 Answers 2

0

You can do this something like this during initialization

char[,,] array3D = new char[2, 2, 3] 
{ 
  { 
    { '0', '0', '0' }, 
    { '0', '0', '0' }
  }, 
  { 
    { '0', '0', '0' }, 
    { '0', '0', '0' }
  }
};

Or just iterate over the array and set the value

for ( int i = 0; i < 2; i++ )
{
  for ( int j = 0; j < 2; j++ )
  {
    for ( int k = 0; k < 3; k++ )
    {
      array3D[i, j, k] = '0';
    }
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Array.GetUpperBound is preferable to literals. Also Array.GetLowerBound to be very scrupulous.
I assume you meant to use a char array and to assign '0' rather than 0 (since assigning 0 like that is pointless because the array will already be initialised to contain all 0 values).
Yes just changed to char array. Thanks for pointing it out
-2

Untested ...

fixed (int* pMyArray= MyArray){
  for (int i = 0; i < 16*16*16; i++)
    *pMyArray + i = VALUE;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.