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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:31:25+00:00 2026-05-25T20:31:25+00:00

Is there a nice way to combine designated initializers from C99, with the result

  • 0

Is there a nice way to combine designated initializers from C99, with the result of a malloc?

The following seems to have needless duplication:

typedef struct {
   int a, b, c;
} Type;

Type *t = malloc(sizeof *t);
*t = (Type) {
    .a = 2,
    .b = 3,
    .c = 5,
};

Can the use of Type, and *t be removed from the above code?

  • 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-25T20:31:26+00:00Added an answer on May 25, 2026 at 8:31 pm

    Since you asked 😉 there is one tool in C to avoid explicit duplication of code, macros. That said I don’t see a way not to repeat at least the name of the type. But in C++ they can’t either, so C is at least as good 🙂

    The easiest I see is

    #define DESIGNATE_NEW(T, ...)       \
      memcpy(malloc(sizeof(T)),         \
             &(T const){ __VA_ARGS__ }, \
             sizeof(T))
    

    which would give

    Type *t = DESIGNATE_NEW(Type,
        .a = 2,
        .b = 3,
        .c = 5,
    );
    

    this has several advantages.

    • It initializes all members correctly, even on architectures with non
      standard representations of the 0 for float types or pointers.
    • Other than Keith’ version it is "coding style" acceptable since it is just an expression that looks like an initialization and anybody should immediately capture visually what the second code snipset is supposed to do.

    NB: Observe the const in the macro, this allows several instances of the compound literal to be folded, if the compiler decides this to be relevant. Also there are means to have a variant where the list of designators is optional, see P99 below.

    The disadvantage is the memcpy and I would be happier with an assignment. Second there is no check for failure of malloc before using the result, but one could probably come across with some weirdness to have the code exit nicely.

    In P99 I go a slightly different way. There we always have an initialization function for a type, something like

    inline
    Type* Type_init(Type* t, int a, int b, int c) {
      if (t) {
        *t = (Type const){ .a = a, .b = b, .c = c };
      }
      return t;
    }
    

    which by macro magic can be made to provide default arguments for a, b and c if they are omitted. Then you can simply use something like

    Type *t = P99_NEW(Type, 1, 2, 3);
    

    in your application code. This is better, since it avoids dereferrencing the pointer when the call to malloc failed. On the other hand this reintroduces an order to the initializers, so not perfect either.

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

Sidebar

Related Questions

Is there a nice way to have Spring's @Controller classes to call a specific
How does one combine (in a nice way) two Scala match'es? First I have
Is there a nice way to convert the timestamp you get from twitter (shown
Using PHP, is there a nice way to get the (parsed) introduction only from
Is there a nice way in the WinAPI to get a path relative to
Is there a nice way in Ext to move elements in DOM? I want
Is there a do until x: ... in Python, or a nice way to
Is there a way to write in Visual Studio in nice formated way the
Just wondering if there was a nice (already implemented/documented) algorithm to do the following
Is there a reasonably simplistic way to combine a trackbar with a progress bar?

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.