Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6150249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:35:25+00:00 2026-05-23T19:35:25+00:00

This is kind of a curiosity question. I do have an existing solution, but

  • 0

This is kind of a curiosity question. I do have an existing solution, but I wonder if people know of a better approach.

My callers want to call me with int[][]. I have a routine that needs to process an int**. What’s the best way to do this conversion? In other words:

public static void Func1(int[][] data) {
  Func2(data); //how to do this?
}

private unsafe static void Func2(int** data) {
  //process data
}

Following is the best approach I could come up with. It works fine, but I can’t say I’m 100% happy with the recursive approach (and the O(data.Length) stack space it requires)

public static void Main() {
  var test=new[] {
    new [] {10},
    new [] {20,30},
    new [] {40,50,60},
    new [] {70,80,90,100},
  };
  MySolution_Func1(test);
}

public unsafe static void MySolution_Func1(int[][] data) {
  var items=new int*[data.Length];
  Recurse(0, data, items);
}

public unsafe static void Recurse(int index, int[][] data, int*[] build) {
  if(index==data.Length) {
    fixed(int** finalp=build) {
      Func2(finalp);
    }
  } else {
    fixed(int* nextp=data[index]) {
      build[index]=nextp;
      Recurse(index+1, data, build);
    }
  }
}

private unsafe static void Func2(int** data) {
  for(var j=0; j<4; ++j) {
    for(var i=0; i<j+1; ++i) {
      Debug.WriteLine("{0},{1}: {2}", j, i, data[j][i]);
    }
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:35:25+00:00Added an answer on May 23, 2026 at 7:35 pm

    There’s no need to copy the whole array. You can create an array of pointers (i.e. IntPtr[]), and then cast that to int**. It’s not pretty, and I wouldn’t suggest doing it. But it can be done. The code below shows how.

    int[][] myArray = new int[10][];
    for (int i = 0; i < 10; ++i)
    {
        int[] x = new int[10];
        for (int j = 0; j < 10; ++j)
        {
            x[j] = 10 * i + j;
        }
        myArray[i] = x;
    }
    
    // Output the array
    Console.WriteLine("int[][]");
    for (int i = 0; i < 10; ++i)
    {
        for (int j = 0; j < 10; ++j)
        {
            Console.Write("{0}, ", myArray[i][j]);
        }
        Console.WriteLine();
    }
    
    // Convert to int*[]
    unsafe
    {
        GCHandle[] handles = new GCHandle[10];
        IntPtr[] ArrayOfPointer = new IntPtr[10];
        for (int i = 0; i < 10; ++i)
        {
            handles[i] = GCHandle.Alloc(myArray[i], GCHandleType.Pinned);
            ArrayOfPointer[i] = handles[i].AddrOfPinnedObject();
        }
        // Okay, let's output that
        Console.WriteLine("int*[]");
        for (int i = 0; i < 10; ++i)
        {
            int* p = (int*)ArrayOfPointer[i];
            for (int j = 0; j < 10; ++j)
            {
                Console.Write("{0}, ", *p);
                ++p;
            }
            Console.WriteLine();
        }
    
        // now convert to int**
        GCHandle bigHandle = GCHandle.Alloc(ArrayOfPointer, GCHandleType.Pinned);
        int** ppInt = (int**)bigHandle.AddrOfPinnedObject();
    
        // and output it
        Console.WriteLine("int**");
        int** pa = ppInt;
        for (int i = 0; i < 10; ++i)
        {
            int* p = *pa;
            for (int j = 0; j < 10; ++j)
            {
                Console.Write("{0}, ", *p);
                ++p;
            }
            Console.WriteLine();
            ++pa;
        }
    
        // Need to free the handles
        bigHandle.Free();
        for (int i = 0; i < 10; ++i)
        {
            handles[i].Free();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this kind of question. In my form, i got this as a
I know this kind of questions have been asked already many times before. The
I am sure that this kind of questions must have been asked before, but
This is just an out of curiosity question. Let's say you have a database
This is kind of a shot-in-the-dark question, but I'm going to ask it anyway
This question is mostly curiosity than anything else. But I currently place all my
This question is kind of general and not very specific. We have a java
Hi I have this kind of strings and I want to obtain Like toto100
I asked this kind of question before, but didn't receive any good answers, perhaps
this kind of follows on from another question of mine. Basically, once I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.