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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:47:51+00:00 2026-05-11T14:47:51+00:00

How can I do something like that (just an example): any_struct *my_struct = create_struct();

  • 0

How can I do something like that (just an example):

any_struct *my_struct = create_struct(); add_struct_member(my_struct, 'a', int_member); add_struct_member(my_struct, 'b', float_member); 

So that I could load and use a struct instance ‘from the outside’ (at the address addressOfMyStruct) with the given structure here?

any_struct_instance *instance = instance(my_struct, addressOfMyStruct); int a = instance_get_member(instance, 'a'); float b = instance_get_member(instance, 'b'); 

I would also like to be able to create struct instances dynamically this way.

I hope it’s clear what I want to do. I know that C/Invoke is able to do it, but is there a separate library to do that?

  • 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-11T14:47:51+00:00Added an answer on May 11, 2026 at 2:47 pm

    Actually demonstrating the code to make this work in C is a bit too involved for an SO post. But explaining the basic concept is doable.

    What you’re really creating here is a templated property bag system. The one thing you’ll need a lot of to keep this going is some assiociative structure like a hash table. I’d say go with std::map but you mentioned this was a C only solution. For the sake of discussion I’m just going to assume you have some sort of hashtable available.

    The ‘create_struct’ call will need to return a structure which contains a pointer to a hashtable which makes const char* to essentially a size_t. This map defines what you need in order to create a new instance of the struct.

    The ‘insance’ method will essentially create a new hashtable with equal number of members as the template hashtable. Lets throw lazy evualation out the window for a second and assume you create all members up front. The method will need to loop over the template hashtable adding a member for every entry and malloc’ing a memory chunk of the specified size.

    The implementation of instance_get_member will simply do a lookup in the map by name. The signature though and usage pattern will need to change though. C does not support templates and must chose a common return type that can represent all data. In this case you’ll need to chose void* since that’s how the memory will need to be stored.

    void* instance_get_member(any_struct_instance* inst, const char* name); 

    You can make this a bit better by adding an envil macro to simulate templates

    #define instance_get_member2(inst, name, type) \   *((type*)instance_get_member((inst),(name))) ... int i = instance_get_member2(pInst,'a', int); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 84k
  • Answers 84k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Obfuscating the value by using the CustomFormat property, using checkbox… May 11, 2026 at 4:58 pm
  • Editorial Team
    Editorial Team added an answer I don't know whether it can be done entirely within… May 11, 2026 at 4:58 pm
  • Editorial Team
    Editorial Team added an answer This blog entry appears to cover things, admittedly from a… May 11, 2026 at 4:58 pm

Related Questions

I have a main page that I want to be a sort of dispatch
Question How can I make sure my application is thread-safe? Are their any common
Suppose I have a number of related classes that all have a method like
I'm trying to create am immutable type (class) in C++, I made it so

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.