谁做个C#中的数组转换成指针啊?我做得这个用fixed语句将数组转换成为指针之后为什么他的地址不是按照顺序来的呢?总共有6个数组成员,他们分别在指针的-4~1这几个地址里面了,而且顺序也变了,请问下谁知道这个怎么解决啊!在此先谢过了!
StructInfo.GLYDENOMINATION[] denomination = new StructInfo.GLYDENOMINATION[6];
GLYCURRENCY ncy0 = new StructInfo.GLYCURRENCY()
{
ulCounts = 1,
ulValue = 1,
Rev = 2,
Category = 4214
};
AddCurrencyID(ncy0.cCurrencyID);
denomination[4].lpCurrencies = &ncy0;
denomination[4].ulArraySize = 7;
StructInfo.GLYCURRENCY ncy1 = new StructInfo.GLYCURRENCY()
{
ulCounts = 1,
ulValue = 5,
Rev = 2,
Category = 4214
};
AddCurrencyID(ncy1.cCurrencyID);
denomination[0].lpCurrencies = &ncy1;
denomination[0].ulArraySize = 7;
StructInfo.GLYCURRENCY ncy2 = new StructInfo.GLYCURRENCY()
{
ulCounts = 1,
ulValue = 10,
Rev = 2,
Category = 4214
};
AddCurrencyID(ncy2.cCurrencyID);
denomination[1].lpCurrencies = &ncy2;
denomination[1].ulArraySize = 7;
StructInfo.GLYCURRENCY ncy3 = new StructInfo.GLYCURRENCY()
{
ulCounts = 1,
ulValue = 20,
Rev = 3,
Category = 4214
};
AddCurrencyID(ncy3.cCurrencyID);
denomination[5].lpCurrencies = &ncy3;
denomination[5].ulArraySize = 7;
StructInfo.GLYCURRENCY ncy4 = new StructInfo.GLYCURRENCY()
{
ulCounts = 1,
ulValue = 50,
Rev = 3,
Category = 4214
};
AddCurrencyID(ncy4.cCurrencyID);
denomination[2].lpCurrencies = &ncy4;
denomination[2].ulArraySize = 7;
StructInfo.GLYCURRENCY ncy5 = new StructInfo.GLYCURRENCY()
{
ulCounts = 1,
ulValue = 100,
Rev = 3,
Category = 4214
};
AddCurrencyID(ncy5.cCurrencyID);
denomination[3].lpCurrencies = &ncy5;
denomination[3].ulArraySize = 7;
fixed (StructInfo.GLYDENOMINATION* denomination0 = denomination)
{
counter[5].Denomination = *denomination0;
}
StructInfo是存放结构体的类
GLYDENOMINATION、GLYCURRENCY是结构体
AddCurrencyID()是给cCurrencyID[]赋值用的。
unsafe private static void AddCurrencyID(byte* CurrencyID)
{
byte[] ID = new byte[4] { 67, 78, 89, 0 };
byte* pSrc = CurrencyID;
fixed (byte* aa = ID)
{
for (int i = 0; i < ID.Length; i++)
{
*(pSrc + i) = *(aa + i);
}
}
}
public struct GLYDENOMINATION
{
public int ulArraySize; //lpCurrencies指向的数组大小
unsafe public GLYCURRENCY* lpCurrencies;// GLYCURRENCY数据结构数组的指针
}
public struct GLYCURRENCY
{
public int ulValue;
public int ulCounts;
public unsafe fixed byte cCurrencyID[4];
public int Rev;
public int Category;
}
把你的代码贴出来看看,不应该出现这样的问题。
我贴到问题补充里面了,请指教!
@陈世鹏: 怎么个乱法?
@笨笨蜗牛: 也没乱,就是感觉跟错位了似的,本来地址不应该是0~5来显示数据的吗,现在他是-4~1来显示数据了!
@陈世鹏: 数据正确就行。前期先满足问题的解决需求。
@笨笨蜗牛: 只是数据正确而已,他的地址不对我也用不到他的数据,我写的这个是给赋值的,不是用来接收值的,所以必须要地址完全正确才行的!