3
function(int[] me)
{
    //Whatever
}

main()
{
    int[,] numbers = new int[3, 2] { {1, 2}, {3, 4}, {5, 6} };    
    function(numbers[1]);
}

So here, I'd want to pass int[] {3,4} to the function... but that doesn't work. Is there a way to do that?

1
  • kind of a confusing question... Commented Feb 19, 2010 at 6:13

5 Answers 5

2

I think you want to use a jagged array instead of a 2d array. There is more information here:

http://msdn.microsoft.com/en-us/library/aa288453%28VS.71%29.aspx

an example from that page is:

int[][] numbers = new int[2][] { new int[] {2,3,4}, new int[] {5,6,7,8,9} };

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

Comments

0

No you can't do that, but you can convert it by yourself. Something like.

 int[,] numbers = new int[3, 2] { {1, 2}, {3, 4}, {8, 6} };

           List<int[]> arrays = new List<int[]>();
           for (int i = 0; i < 3; i++)
           {
               int[] arr = new int[2];
               for (int k = 0; k < 2; k++)
               {
                   arr[k] = numbers[i, k];
               }

               arrays.Add(arr);
           }

           int[] newArray = arrays[1]; //will give int[] { 3, 4}

1 Comment

well i guess i took the question too literally then..eh?
0
function(int[] me) 
{ 
    //Whatever 
} 

main() 
{ 
    int[][] numbers = new int[3][] { new int[2] {1, 2}, new int[2]{3, 4}, new int[2] {5, 6} };     
    function(numbers[1]); 
} 

This is called a jagged array (an array of arrays). If you can't change the definition of numbers, you would need to write a function which can

Comments

0

int[] {3,4} refers to a location, not an array and holds an int. Your function should be

function(int me)
{
    //Whatever
}

this is how you will get value and pass to your function

    int valueFromLocation = numbers[3,4];

    function(valueFromLocation )
    {
        //Whatever
    }

EDIT:

if you need whole array in any case use Jagged Arrays

int[][] jaggedArray =
     new int[2][] { new int[] {2,3,4}, new int[] {5,6,7,8,9} };

int[] array1 =  jaggedArray[1];
int[] array1 =  jaggedArray[2];

now you can pass it the way you want

function(int[] array){}

2 Comments

he wants to pass a int[], that's the question.
Thanks, " int[] {3,4} " was confusing. have fixed my answer now
0

Perhaps use function(new int[] { 3, 4 }); ? It does help if you write an actual question.

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.