如何用C# 实现共用体,共用体中包括一个20字节结构体、char数组
发布网友
发布时间:2024-10-09 02:45
我来回答
共2个回答
热心网友
时间:2024-11-13 23:24
试了几下没成功orz
将就一下用指针怎么样?
using System;
using System.Runtime.InteropServices;
class Program
{
[StructLayout(LayoutKind.Sequential)]
struct Struct20B
{
public int a;
public int b;
public int c;
public int d;
public int e;
}
static void Main(string[] args)
{
Struct20B s20b;
s20b.a = 0;
s20b.b = 2;
s20b.c = 8;
s20b.d = 15;
s20b.e = 18;
unsafe
{
byte* pStruct = (byte*) &s20b;
for (int i = 0; i < 20; ++i)
Console.WriteLine(pStruct[i]);
}
}
}
热心网友
时间:2024-11-13 23:25
[ StructLayout( LayoutKind.Explicit )]
public struct MyUnion
{
[ FieldOffset( 0 )]
public int i;
[ FieldOffset( 0 )]
public double d;
}