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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:54:37+00:00 2026-05-27T14:54:37+00:00

How will I dynamically allocate an array of struct Register with each register having

  • 0
  1. How will I dynamically allocate an array of struct Register with each register having a dynamically allocated array of struct Field ?
  2. How will I access each of these members (Can I use Register_A.FieldArray[23].High?)?
  3. How will I initialize register structure efficiently(assuming that register struct array is a big one) ? (Like a data segment without spending any compute)

    struct Field
    {
        char    High;                
        char    Low;                
        char    Attribute;
    };
    
    struct Register 
    {
       unsigned int    ResetValue;
       struct Field    FieldArray[];
    };
    
  • 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-27T14:54:38+00:00Added an answer on May 27, 2026 at 2:54 pm

    I strongly discourage you from using a flex array, because they’re nothing but a kludge. You should instead declare and use it as a struct Field *, mallocing it to size like any other dynamic array.

    That said, to malloc with an array size of n_elem:

    struct Register register = malloc(sizeof(*register) + n_elem * sizeof(*register->FieldArray));
    

    To access the elements:

    char high = register->FieldArray[0].High;
    

    For initialization: in gcc, at least, you can initialize the array part statically like any other static array. I’m not sure how other compilers handle it.


    Why flex arrays are discouraged:

    I’ll link this post, but I don’t think the answer is complete (most of the discouragement is because it’s c-99 only), so I’ll add my thoughts.
    They are an exception. In essence, you’re declaring the array to be of zero size and accessing it out of bounds every time, just to an extent you have allocated for. You can’t use a struct with a flexible array just like any other struct, e.g. you can’t take the sizeof the flexible array. If you declare a static one or an array of them, you can’t use the array member because there’s no space allocated for it.

    Example of using them in an array:

    #include <stdio.h>
    
    struct test {
            int val;
            int arr[];
    };
    
    int main() {
            struct test tarr[2];
            printf("%p\n%p\n", &tarr[0].arr[0], &tarr[1].val);
    }
    

    Output:

    0x7fff59b67164
    0x7fff59b67164
    

    They are at the same address. If you try to write to the array member, you overwrite the next object. If you try to read from it, you read data from the next object. Depending on padding, this could even be a nonsense value from the padding.

    They’re like goto statements, both are potentially useful but more often a bad idea. If you don’t know why they’re dangerous, you shouldn’t be using them (in real code; it’s not a bad idea to play around with them in a test program to see how to use them correctly and how they can introduce problems).

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

Sidebar

Related Questions

If i use auto_ptr to hold a pointer to a dynamically allocated array, when
Will the free() routine work if I dynamically allocate an array and then pass,
How can you reliably and dynamically load a JavaScript file? This will can be
I have a 2 dimensional array dynamically allocated in my C code, in my
I'm building an application which will have dynamic allocated objects of type A each
I have a dynamically allocated array : myRectangle lastRectanglesArray = new myRectangle[lastMaxLabel]; I would
If you have a statically allocated array, the Visual Studio debugger can easily display
I have c++ code that attempts to dynamically allocate a 2d array of bytes
I wish to dynamically allocate set of objects (can be several hundreds). Part of
I'm building a simple list using a dynamically allocated array in C++. I have

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.