Gio
Gio

Reputation: 4229

How to create an array in a structure that I need to get a pointer?

I need to create an array inside a structure for the purpose of connecting to an embedded device. As such we using pointers to both the structure and it's internal fields...I tried the code below but should i just create 100 ints and be done??

[StructLayout( LayoutKind.Sequential )]
public struct HRTF
{
    UInt32 PPP;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 100)]
    UInt32[] taps;
    . . . 
}

. . . 

HRTF effects = new HRTF();

/* --- ERROR cannot get address of, Sizeof ect ..to unmanaged typE 'HRTF' */
int offset = ((int)&effects.taps - startOffset) / 4; 
int length = sizeof(HRTF) / 4;

Upvotes: 1

Views: 107

Answers (1)

YoryeNathan
YoryeNathan

Reputation: 14522

fixed (uint* pt = effects.taps)
{
    // pt is not pointer to taps
}

var bts = Marshal.SizeOf(effects); // bts has size of HRTF in bytes.

OR

var bts = Marshal.SizeOf(typeof(HRTF));

Upvotes: 1

Related Questions