15,995,305 members
See more:
Hello
I am trying to convert a 2d CLR array to a single pointer array in C++. here is my code:
C++
```array<array<double>^>^ myarray =
{
{1,2,3,4},
{5,6,7,8}
};
pin_ptr<double> p = &myarray[0][0];
double* R = p;

for (size_t i = 0; i < 2; i++)
{
for (size_t j = 0; j < 4; j++)
{
Console::WriteLine(R[i, j]);
}
}```

The problem is that R hold only the first row of my 2d array....
if I do the same thing in normal c++ The
`R`
variable hold my entire array

How can I solve this problem ?

What I have tried:

I really do not know how to solve this issue...
Posted
Updated 2-Feb-21 21:14pm
Shao Voon Wong 2-Feb-21 7:13am
Why do you want to pin it?
There is a mathematical library in c++ called Armadillo, in order for me to use this I must convert my 2d array to a single pointer double* and then use Armadillo...
11917640 Member 2-Feb-21 9:36am
You need to pin pointer for every line.
How should I do it ?
11917640 Member 2-Feb-21 9:49am
myarray in your code is jagged array. See the difference here: https://docs.microsoft.com/en-us/cpp/dotnet/how-to-use-arrays-in-cpp-cli?view=msvc-160 multi-dimension managed array vs jagged array.

## Solution 1

You do not need to pin. Just assign one element at a time to the native mat variable, `R`.

C++
```array<array<double>^>^ myarray =
{
{1,2,3,4},
{5,6,7,8}
};

mat R(2,4);

for (size_t i = 0; i < 2; i++)
{
for (size_t j = 0; j < 4; j++)
{
R(i, j) = myarray[i][j];
}
}```

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 Dave Kreskowiak 45 OriginalGriff 25 RickZeeland 10 Richard Deeming 10
 OriginalGriff 431 Pete O'Hanlon 215 Dave Kreskowiak 115 CPallini 110 CHill60 42

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900