Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6994089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:50:50+00:00 2026-05-27T19:50:50+00:00

Want to do this: (EDIT: bad sample code, ignore and skip below) struct RECORD

  • 0

Want to do this:
(EDIT: bad sample code, ignore and skip below)

struct RECORD {
    char[] name = new char[16];
    int dt1;
}
struct BLOCK {
    char[] version = new char[4];
    int  field1;
    int  field2;
    RECORD[] records = new RECORD[15];
    char[] filler1 = new char[24];
}

But being unable to declare array sizes in struct, how do I reconfigure this?

EDIT: The reason for the layout is I’m using BinaryReader to read a file written with C structs. Using BinaryReader, and a C# struct union (FieldOffset(0)), I’m wanting to load the header as a byte array, then read it as it was intended originally.

[StructLayout(LayoutKind.Sequential)]
unsafe struct headerLayout
{
    [FieldOffset(0)]
    char[] version = new char[4];
    int fileOsn;
    int fileDsn;
    // and other fields, some with arrays of simple types
}

[StructLayout(LayoutKind.Explicit)]
struct headerUnion                  // 2048 bytes in header
{
    [FieldOffset(0)]
    public byte[] headerBytes;      // for BinaryReader
    [FieldOffset(0)]
    public headerLayout header;     // for field recognition
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:50:51+00:00Added an answer on May 27, 2026 at 7:50 pm

    I wouldn’t use that pattern in the first place. This kind of memory mapping may be appropriate in c, but not in a high level language like C#.

    I’d just write a call to the binary reader for each member I want to read. This means you can use classes and write them in a clean high level way.

    It also takes care of endian issues. Whereas memory mapping will break when used on different endian systems.

    Related question: Casting a byte array to a managed structure


    So your code would look similar to the following (add access modifiers etc.):

    class Record
    {
        char[] name;
        int dt1;
    }
    class Block {
        char[] version;
        int  field1;
        int  field2;
        RECORD[] records;
        char[] filler1;
    }
    
    class MyReader
    {
        BinaryReader Reader;
    
        Block ReadBlock()
        {
            Block block=new Block();
            block.version=Reader.ReadChars(4);
            block.field1=Reader.ReadInt32();
            block.field2=Reader.ReadInt32();
            block.records=new Record[15];
            for(int i=0;i<block.records.Length;i++)
                block.records[i]=ReadRecord();
            block.filler1=Reader.ReadChars(24);
            return block;
        }
    
        Record ReadRecord()
        {
            ...
        }
    
        public MyReader(BinaryReader reader)
        {
            Reader=reader;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this html code that i want to edit with jQuery. Here is
I'm new to PHP and I want this code <option value= disabled=disabled>-------------</option> to be
I am a Cake PHP novice. I want to edit the table Issue. This
I want this for some conditional compilation code that will run in all IE's
I want this url http://www.youtube.com/watch?v=dgNgODPIO0w&feature=rec-HM-fresh+div to be transformed to: http://www.youtube.com/v/dgNgODPIO0w with php.
I want this: http://jqueryui.com/demos/dialog/#modal-message to happend when you click on ClickMe. how to do
I want this to be on top... how can I do this and not
I only want this function to run if .toolbar li does not have the
Before I ask this, do note: I want this for debugging purposes. I know
Here's a simple (hopefully) L10N question: Do all locales want this format: Sunday, Nov

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.