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 6116565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:13:13+00:00 2026-05-23T15:13:13+00:00

Is it able to define a variable length struct in gnuc to represent a

  • 0

Is it able to define a variable length struct in gnuc to represent a object as follow:

field1: fixed 4bytes;
field2: length of field3
field3: variable length
field4: length of field5
field5: variable length
field6: fixed 8bytes
field7: fixed 1byte

I know in gnuc we can use the zero-size array to implement a variable length struct, e.g.

typedef struct varStruct{
  int foo1;
  int foo2[0];
}varStruct;

But the above usage requires the variable length field placed at the tail of the struct.
What if they are in the middle?

  • 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-23T15:13:14+00:00Added an answer on May 23, 2026 at 3:13 pm

    You can’t have a struct with more than one variable array or a variable array in the middle. Think about it, how would the compiler know where field3 and field4 start if the length field2 is variable?

    If field1 contains the length of the next two fields, you could read the members of the struct manually. Example code (read it as pseudocode):

    #define PADDING 8 /* or perhaps sizeof(void *) */
    
    typedef struct {
       int32_t field_len;
       char data[0];
    } main_str_t;
    
    typedef struct {
       int64_t one;
       int8_t another;
    } tail_str_t;
    
    ....
    
    main_str_t *data = get_data();
    int32_t len = data->field_len;
    int32_t padded_len = ((field_len + PADDING - 1) / PADDING) * PADDING;
    char *field2 = data->data;
    char *field3 = field2 + padded_len;
    tail_str_t *tail = field3 + padded_len;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given that users must be able to define their own fields on an object,
Essentially, I'd love to be able to define a variable as one thing unless
I would like to be able to define and use a custom type in
In my application users should be able to define dates when they are available.
I'd like to be able to define lambdas using common Lisp syntax, in Clojure.
I'd like to be able to use this.type to define a method that makes
So I have something like this #define HASHSIZE 1010081 static struct nlist *hashtab[HASHSIZE]; Now
I have an array of objects of variable length n. Defined by the number
How can I define a global javascript variable in this situation? I want the
I wish to define a type String such that declaring a variable of type

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.