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

The Archive Base Latest Questions

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

I’m pretty new at C programming, and this type of thing keeps popping up.

  • 0

I’m pretty new at C programming, and this type of thing keeps popping up. As a simple example, suppose I have a struct http_header with some char pointers:

struct http_header {
    char* name;
    char* value;
};

I want to fill an http_header where value is the string representation of an int. I “feel” like, semantically, I should be able to write a function that takes in an empty header pointer, a name string, and an int and fills out the header appropriately.

void fill_header(struct http_header *h, char* name, int value)
{
    h->name = name;
    char *value_str = malloc(100);
    sprintf(value_str, "%d", value);
    h->value = value_str;
}

int main(int argc, const char * argv[])
{
    struct http_header h;
    char *name = "Header Name";
    int val = 42;
    fill_header(&h, name, val);
    ...
    free(h.value);
}

Here, the calling code reads exactly as my intent, but in this case I’m creating the value string dynamically, which means I’d have to free it later. That doesn’t smell right to me; it seems like the caller then knows too much about the implementation of fill_header. And in actual implementations it may not be so easy to know what to free: consider filling an array of http_headers where only one of them needed to have its value malloced.

To get around this, I’d have to create the string beforehand:

void fill_header2(struct http_header *h, char* name, char *value_str)
{
    h->name = name;
    h->value = value_str;
}

int main(int argc, const char * argv[])
{
    struct http_header h;
    char *name = "Header Name";
    int value = 42;

    char value_str[100];
    sprintf(value_str, "%d", value);
    fill_header2(&h, name, value_str);
}

As this pattern continues down the chain of structures with pointers to other structures, I end up doing so much work in top level functions the lower level ones seem hardly worth it. Furthermore, I’ve essentially sacrificed the “fill a header with an int” idea which I set out to write in the first place. I’m I missing something here? Is there some pattern or design choice that will make my life easier and keep my function calls expressing my intent?

P.S. Thanks to all at Stackoverfow for being the best professor I’ve ever had.

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

    Well, I would go with the first approach (with a twist), and also provide a destroy function:

    struct http_header *make_header(char *name, int value)
    {
        struct http_header *h = malloc(sizeof *h);
        /* ... */
        return h;
    }
    
    void destroy_header(struct http_header *h)
    {
        free(h->name);
        free(h);
    }
    

    This way the caller doesn’t have to know anything about http_header.

    You might also get away with a version that leaves the main allocation (the struct itself) to the caller and does it’s own internal allocation. Then you would have to provide a clear_header which only frees that fill allocated. But this clear_header leaves you with a partially-valid object.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text

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.