Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Comments
Shao Voon Wong 2-Feb-21 7:13am    
Why do you want to pin it?
MohammadrezaMC2 2-Feb-21 7:27am    
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.
MohammadrezaMC2 3-Feb-21 0:36am    
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.

1 solution

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];
    }
}
 
Share this answer
 

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



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