简体   繁体   中英

Converting array of pointers from C++ to C#

I am C++ programmer, and I am stuck in a migration project where I need to convert below C++ code to C#. Need help on the same.

unsigned short** varData = new unsigned short*[ndata]; //Say ndata is 10

for(int i=0; i<ndata; i++) varData[i] = new ushort[nwp]; 

Thanks in advance.

This gives you a 2D array of 16bit unsigned integers, which is probably what you try to express in C++ by using pointer to pointer.

  int nData = 10;
  int nwp = 3;

  var varData = new UInt16[ nData, nwp ]; //varData is of type UInt16[,]

http://msdn.microsoft.com/en-us/library/2yd9wwz4(v=vs.71).aspx

If I understand correctly, you use unsigned short** to store 2-dimensional array of ushort. In C# you can just declare it as

ushort[,] array = new ushort[m, n];

where m and n are the dimensions.

Use Jagged Arrays, not 2D array; if you need the same result as C++. See http://msdn.microsoft.com/en-us/library/2s05feca.aspx for more informations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM