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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:14:16+00:00 2026-06-11T16:14:16+00:00

I have to write a function update(char* name, void* data); which receives the name

  • 0

I have to write a function update(char* name, void* data); which receives the name of the message and void* to the data of that message. The type of the data is always fixed as “xxx_name”, depending on what the name is.

My function should do the following :-

  1. Generate a new structure of type of xxx_name within the function.
  2. Copy the data from the pointer data, of size xxx_name to a new location, as data would soon be freed by external interface.

In my case, the list of possible structures of type xxx_name is pretty huge and spread across, so writing if/else or switch conditions is out of question.

Is there a way to do this in C via some macro / stringizing operator etc?

  • 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-06-11T16:14:17+00:00Added an answer on June 11, 2026 at 4:14 pm

    This solution uses NULL terminated look up table (LUT) to find the appropriate size. Then allocates memory in that size. Then copies the input data to the new object. If the number of types of objects is large enough the LUT can be presorted then a binary search performed instead of a linear search.

    #include<stdlib.h>
    #include<string.h>
    
    struct nameToSize_s {
        const char *name;
        size_t size;
    };
    
    struct foo1_name_s {
    };
    
    struct foo2_name_s {
    };
    
    const struct nameToSize_s nameToSizeLUT[] = {
        { "foo1_name",  sizeof(struct foo1_name_s) },
        { "foo2_name",  sizeof(struct foo2_name_s) },
        // NULL terminator
        { NULL, 0}
    };
    
    void * update(char* name, void *data) {
        const struct nameToSize_s *entry = nameToSizeLUT;
    
        while(entry->name) {
            if( strcmp(entry->name, name) == 0) break;
            entry++;
        }
    
        if( entry->name == NULL) abort();
    
        void *newObj = malloc(entry->size);
        if( newObj == NULL) abort();
    
        return memcpy( newObj, data, entry->size);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to write a swap function for my bubble sort That's what I've
I have a simple file read and write function. private void WriteToFile(String filename, String
I'd like to write a function that would have some optional code to be
I have problem with fancybox. I want to write a function that will run
I have a function that calls out a read or write request on a
I have a web page which has 6 checkboxes on it. <input type=checkbox name=chk1
I have the following javascript code, which loads without error, however the update function
In my current work, I have to write a replace function. A replace function,
I have to write my own hash function. If I wanted to just make
how to write a function from list to a tuple i have taken the

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.