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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:02:52+00:00 2026-05-23T16:02:52+00:00

Please have a look at the following code: #include <stdio.h> #include <stdlib.h> typedef struct

  • 0

Please have a look at the following code:

#include <stdio.h>
#include <stdlib.h>

typedef struct {
        const char * cmd;
        const char * help;
} CmdEnum;

static CmdEnum cmd_enum[] = {
        {"help", "This help..."},
        {"first", "The first command"},
        {"second", "The second command"},
};

void main()
{
    int i,n;
    char *out = "";

        n = sizeof(cmd_enum) / sizeof(CmdEnum);
        for (i=0; i<n; i++)
        {
            char *oldOut = out;
            CmdEnum cmd = cmd_enum[i];
            asprintf(&out, "%s%s -> %s\n", oldOut, cmd.cmd, cmd.help);
            if(i>0) free(oldOut);
        }

    printf("%s", out);
    printf("Done.\n");
}

Is this a good way to build a text from the CmdEnum?
Is there a “nicer” way do define cmd in the first place as to avoid the if(i>0) free...?
Or am I doing something entirely wrong?

EDIT:
After reading larsmans’ answer I modified main to:

int main()
{
    int i,n, copied, siz;
    char *out, *cursor;
    siz = 1;// 1 for NUL char
    n = sizeof(cmd_enum) / sizeof(CmdEnum);

    for (i=0; i<n; i++)
    {
        siz += strlen(cmd_enum[i].cmd) + strlen(cmd_enum[i].help) + strlen(":\n\t\n\n");
    }

    out = malloc(siz);
    if(!out)
    {
        printf("Could not alloc!\n");
        return 1;
    }

    cursor = out;
    for (i=0; i<n; i++)
    {
        copied = snprintf(cursor, siz, "%s:\n\t%s\n\n", cmd_enum[i].cmd, cmd_enum[i].help);
        if(copied < 0 || copied >= siz)
        {
            printf("snprintf failed: %i chars copied.\n", copied);
            return 1;
        }

        cursor += copied;
        siz -= copied;
    }

    printf("%s", out);
    printf("Done.\n");
    free(out);
    return 0;
}

(Note: I also changed the output format…)

  • 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-23T16:02:53+00:00Added an answer on May 23, 2026 at 4:02 pm

    Is this a good way to build a text from the CmdEnum?

    Yes, except that asprintf is not portable (although you can define it easily in terms of snprintf for platforms that don’t have it) and you’re not checking error returns. void main isn’t valid C btw.

    Is there a “nicer” way do define cmd in the first place as to avoid the if(i>0) free…?

    You could allocate the whole string beforehand.

    size_t i, siz = 1;  // 1 for NUL char
    for (i=0; i<n; i++)
        siz += strlen(cmd_enum[i].cmd) + strlen(cmd_enum[i].help) + strlen(" -> \n");
    
    char *out = malloc(siz);
    // check for errors
    

    then build the string with snprintf. This saves you some malloc‘ing and error checking in the loop.

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

Sidebar

Related Questions

Please have a look at following code : import java.util.ArrayList; import java.util.List; class Main{
please have a look at the following code. When the value of i ==
Please have look on the following code: $_SESSION[process_y] = new Process(); $process_y = $_SESSION[process_y];
I have some thread-related questions, assuming the following code. Please ignore the possible inefficiency
I have the following code: #include <iostream> #include <string> void main() { std::string str;
Have a look at the following code you are not required to read the
Please take a look at the following autocomplete code. $(document).ready(function(){ $(#the_input).autocomplete(/autocomplete.php, { //some options
I have just started to look at .NET 3.5 so please forgive me if
Please help! Background info I have a WPF application which accesses a SQL Server
I might have wandered into wrong direction so please help. I have a page

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.