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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:30:40+00:00 2026-05-20T05:30:40+00:00

The C99 standard allows the creation of flexible array members such as typedef struct

  • 0

The C99 standard allows the creation of flexible array members such as

typedef struct pstring {
  size_t length;
  char   string[];
} pstring;

This is then initialized with something like pstring* s = malloc(sizeof(pstring) + len). Is it permissible for len to be zero? It would seem consistent, and would be nice for saving space from time to time (probably not with the pstring example, of course). On the other hand, I have no idea what the following code would do:

pstring* s = malloc(sizeof(pstring));
s->string;

This also seems like the sort of thing which might work with one compiler and not another, or on one OS and not another, or on one day and not another, so what I really want to know is what the standard says about this. Is that malloc in the example code undefined behavior, or is it only the access to s->string which is invalid, or is it something else entirely?

  • 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-20T05:30:41+00:00Added an answer on May 20, 2026 at 5:30 am

    What you do is valid, but accessing s->string[0] or feeding s->string to any function accessing the data is invalid.

    The C99 Standard actually says (§6.7.2.1):

    struct s { int n; double d[]; };

    …

    struct s t1 = { 0 }; // valid

    …

    The assignment to t1.d[0] is probably undefined behavior, but it is possible that

    sizeof (struct s) >= offsetof(struct s, d) + sizeof (double)

    in which case the assignment would be legitimate. Nevertheless, it cannot appear in strictly conforming
    code.

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

Sidebar

Related Questions

I would like to have a string (char*) parsed into a tm struct in
Speaking of string literals, the C99 standard says (6.4.5.6): It is unspecified whether these
Are there machines (or compilers), where sizeof(char) != 1 ? Does C99 standard says
How universally is the C99 standard supported in today's compilers? I understand that not
As per c99 standard, size of long long should be minimum 64 bits. How
Thankfully, the complex type modifier was introduced into C99 standard. What I don't understand
So, poking through the n869 draft of the C99 standard I stumbled across this
I believe that in C99, modification of string literals is undefined behaviour. I don't
Paragraph 6.7.3.8 of the C99 spec states If the specification of an array type
The C99 Standard says in $6.5.2. Between the previous and next sequence point an

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.