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

  • Home
  • SEARCH
  • 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 9211499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:18:39+00:00 2026-06-18T01:18:39+00:00

We can initialize a struct with zero length array as specified in the link:

  • 0

We can initialize a struct with zero length array as specified in the link:

Zero-Length.

I’m using the following structures:

typedef unsigned char UINT8;
typedef unsigned short UINT16;

typedef struct _CommandHeader
{
    UINT16 len;
    UINT8 payload[0];
} CommandHeader;

typedef struct _CmdXHeader
{
    UINT8 len;
    UINT8 payload[0];
} CmdXhHeader;

Now the CommandHeader.payload should point / contain to CmdXHeader struct. i.e. memory should look like:

 -------------------------------------------------------------
| CommandHeader.len | CmdXHeader.len | CmdXHeader.payload ....|
 -------------------------------------------------------------

I can easily malloc CmdXHeader / CommandHeader to customized length. But how to assign value to CmdXHeader payload or how to link a CmdXHeader object to the CommandHeader.payload?


My Solution

Thanks for all the reply. I solved it in the following way:

//Get the buffer for CmdXHeader:
size_t cmdXHeader_len = sizeof(CmdXHeader) + custom_len;
CmdXHeader* cmdXHeader = (CmdXHeader*) malloc(cmdXHeader_len);
//Get a temporary pointer and assign the data to it
UINT8* p;
p[0] = 1;
p[2] = 2;
.......

//Now copy the memory of p to cmdXHeader
memcopy(cmdHeader->payload, p, custom_len);

// allocate the buffer for CommandHeader
CommandHeader* commandHeader = (CommandHeader*) malloc (sizeof (CommandHeader) + cmdXHeader_len);

// populate the fields in commandHeader
commandHeader->len = custom_len;
memcpy(commandHeader->payload, cmdXHeader, cmdXHeader_len);

Now the commandHeader object have the desired memory and we can typecast with whatever way we want…

  • 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-06-18T01:18:41+00:00Added an answer on June 18, 2026 at 1:18 am

    A zero-length array at the end of a struct, or anywhere else, is actually illegal (more precisely a constraint violation) in standard C. It’s a gcc-specific extension.

    It’s one of several forms of the “struct hack”. A slightly more portable way to do it is to define an array of length 1 rather than 0.

    Dennis Ritchie, creator of the C language, has called it “unwarranted chumminess with the C implementation”.

    The 1999 revision of the ISO C Standard introduced a feature called the “flexible array member”, a more robust way to do this. Most modern C compilers support this feature (I suspect Microsoft’s compiler doesn’t, though).

    This is discussed at length in question 2.6 of the comp.lang.c FAQ.

    As for how you access it, whichever form you use, you can treat it like you’d treat any array. The name of the member decays to a pointer in most contexts, allowing you to index into it. As long as you’ve allocated enough memory, you can do things like:

    CommandHeader *ch;
    ch = malloc(computed_size);
    if (ch == NULL) { /* allocation failed, bail out */ }
    ch.len = 42;
    ch.payload[0] = 10;
    ch.payload[1] = 20;
    /* ... */
    

    Obviously this is only a rough outline.

    Note that sizeof, when applied to the type CommandHeader or an object of that type, will give you a result that does not include the flexible array member.

    Note also that identifiers starting with underscores are reserved to the implementation. You should never define such identifiers in your own code. There’s no need to use distinct identifiers for the typedef name and the struct tag:

    typedef struct CommandHeader
    {
        UINT16 len;
        UINT8 payload[0];
    } CommandHeader;
    

    I’d also suggest using the standard types uint16_t and uint8_t, defined in <stdint.h> (assuming your compiler supports it; it’s also new in C99).

    (Actually the rules for identifiers starting with underscores are slightly more complex. Quoting N1570, the latest draft of the standard, section 7.1.3:

    • All identifiers that begin with an underscore and either an uppercase letter or another
      underscore are always reserved for any use.
    • All identifiers that begin with an underscore are always reserved for use as identifiers
      with file scope in both the ordinary and tag name spaces.

    And there are several more classes of reserved identifiers.

    But rather than working out which identifiers are safe to use at file scope and which are safe to use in other scopes, it’s much easier just to avoid defining any identifiers that start with an underscore.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I initialize this nested struct in C? typedef struct _s0 { int
In C, you can partially initialize a struct or array, with the result that
We can initialize a container deque by using standard input like this: deque<int> c((istream_iterator<int>(cin)),(istream_iterator<int>()));
Using C# 3.0, we can initialize objects without their constructors for syntactical reasons. Such
I can initialize float32x4_t like this: const float32x4x4_t zero = { 0.0f, 0.0f, 0.0f,
I have the following class definitions in c++: struct Foo { int x; char
Can I build a constructor to initialize a struct this way: mystruct struct1(a,b); the
It is possible to do something like this How can I initialize an array
can someone explain me the compilation error in this code: #include common.h typedef struct
Possible Duplicate: How do I initialize a member array with an initializer_list? You can

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.