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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:36:12+00:00 2026-05-11T03:36:12+00:00

Is there a way to have some kind of default constructor (like a C++

  • 0

Is there a way to have some kind of default constructor (like a C++ one) for C user types defined with a structure?

I already have a macro which works like a fast initializer (like the one for pthread_mutex) but I wanted to know if you can by any chance have some (or all) fields of a struct filled at declaration.

For instance, with the pthread_mutex example, I would like

pthread_mutex_t my_mutex; 

to have the same effect as

pthread_mutex_t my_mutex = PTHREAD_MUTEX_INITIALIZER; 
  • 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. 2026-05-11T03:36:13+00:00Added an answer on May 11, 2026 at 3:36 am

    You can create initializer functions that take a pointer to a structure. This was common practice.

    Also functions that create a struct and initialize it (like a factory) – so there is never a time where the struct is ‘uninitialized’ in the ‘client’ code. Of course – that assumes people follow the convention and use the ‘constructor’/factory…

    horrible pseudo code with NO error checking on malloc or free

    somestruct* somestruct_factory(/* per haps some initializer agrs? */) {   malloc some stuff   fill in some stuff   return pointer to malloced stuff }   void somestruct_destructor(somestruct*) {   do cleanup stuff and also free pointer   free(somestruct); } 

    Someone will probably come along and explain how some early C++ preprocessors/compilers worked to do this all in C.

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

Sidebar

Related Questions

If we have some branch, B1, is there a way to create branch B2
I have some VirtualBox VMs running. Is there any way to programatically get the
I have some code to read from a pdf file. Is there a way
Is there a way to delay css from firing using jquery? I have some
Is there some way when sending this message to specify that I rather have
I have a question about SqlDataReader: Is there some way to modificated the values
Is there some way to replicate rails' link-to-unless-current? Ie. if i have a list
If I have an NSArray with some values in them. Is there a way
I have a data() object containing some json. Is there a way I can
I have a asp.net mvc application. Some pages require SSL, is there a way

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.