Click here to Skip to main content
12,356,872 members (64,227 online)

Fixed size array of type struct as memeber of another struct

Andy411 asked:

Open original thread
Hi folks!

I have to send and receive data via TCP/IP from a legacy server. The bytes I send and receive are of course representing some data structures. In C/C++ I would memcpy the array into an existing structure or I would just cast a pointer of the strcut type to my byte array. In C it would look something like this:
#pragma pack(push, 1)

typedef struct INNER_ST
{
    DWORD A;
    BYTE B;
};
 
typedef struct FOO_ST
{
    WORD W;
    BYTE X[20];
    INNER_ST data[10];
};
 
#pragma pack(pop, 1)

void ReceiveData(const BYTE *pData)
{
    FOO_ST *pFooSt;
 
    pFooSt = (FOO_ST *)pData;
 
    DWORD alpha = pFooSt->data[0].A;
 
}


In C# my structures would look like this:
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct INNER_ST
{
    public UInt32 A;
    public Byte B;
};
 
[StructLayout(LayoutKind.Sequential, Pack = 1)]
unsafe struct FOO_ST
{
    public UInt16 W;
    public fixed Byte X[20];
    public fixed INNER_ST data[10]; // <- fails, Error CS1663 
    /*
    "Fixed size buffer type must be one of the following: bool, byte, short, int, long, char, sbyte, ushort, uint, ulong, float or double"
    */
};

To copy "simmple" structures like INNER_ST with Marshal.PtrToStructure is not the problem.

My question: What can I do with arrays of structures inside structure to solve error CS1663.

Thx for any ideas or hints.
Andy

PS: Of course I cannot change the server side. I have the header files with the structure definitions in C and the new client will be in C#.
Tags: C#, Unmanaged

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).



Advertise | Privacy | Mobile
Web01 | 2.8.160621.1 | Last Updated 26 Mar 2009
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100