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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:21:03+00:00 2026-05-16T16:21:03+00:00

I need to store the following strings in 3 char * variables in C:

  • 0

I need to store the following strings in 3 char * variables in C:

[foo]
[foo]:[bar]
[foo]:[bar]:[tat]

Each individual string such as “foo” is received at one point of time via a char* pointer, the corresponding output string is produced immediately before receiving the second string, and the total number of individual strings is known at all times.

There can be an arbitrary number of strings.

I wrote the following code to print the strings:

for(i=0;i<num;i++)
    {  
        for(j=0;j<=i;j++)
            {
                printf("[%s]",str[j]);
                if(j!=i)
                {
                    printf(":");
                }
                else
                {
                    printf("\n");
                }
            }       
    }   

But I am unable to figure out a way of storing the desired output strings in variables, other than writing a messy case-by-case function using strcat() & some additional self-defined functions.

Is there a way to do this, that looks as clean as simply printing the strings?

  • 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-16T16:21:03+00:00Added an answer on May 16, 2026 at 4:21 pm

    This is a memory-management-and-string-handling-in-C-are-tedious problem. If I’m right in thinking that for n input strings, you want n output strings, each longer than the last, I’d do this:

    char **results = malloc(num * sizeof(char*));
    if (results == 0) return ENOMEM;
    int lastlen = 0;
    for (int i = 0; i < num; ++i) {
        char *newresult = malloc(lastlen + strlen(str[i]) + 4);
        if (newresult == 0) { /* even more tedious freeing code goes here */ }
        results[i] = newresult;
        char *ptr = newresult;
        if (i != 0) {
            ptr += sprintf(ptr, "%s:", results[i-1])
        }
        sprintf(ptr, "[%s]", str[i])
        lastlen = strlen(newresult);
    }
    

    Or something like that. Obviously there’s a good opportunity to write more than one function here, to separate the generation of these strings from the storing of them in arrays. There’s also the usual argument how we can do better in the risk-of-buffer-overflow stakes than sprintf + mental arithmetic.

    Edit: stole the use of sprintf from Oskar.

    Another edit: example with asprintf, which is available on GNU and BSD. This has the tedious freeing code included, the success path is actually quite good. Returns a newly-allocated array on success, 0 on error. The caller is assumed to know num and therefore know how long the array will be: if that’s no use then the array could be null-terminated.

    char **results = malloc(num * sizeof(char*));
    if (results != 0) {
        const char *last = "";
        const char *sep = "";
        for (int i = 0; i < num; ++i) {
            if (asprintf(results + i, "%s%s[%s]", last, sep, str[i]) == -1) break;
            last = str[i];
            sep = ":";
        }
        if (i == num) return results; // success!
        // asprintf failed, so the value of results[i] is undefined
        while (i != 0) free(results[--i]);
        free(results); 
    }
    return 0;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine the following: you read in a string with scanf() but you only need
I need to store the following data; Clampls = {23e23e, ff333g, fhgswq, h65h3, ffwwf,
I need to get the ASCII character for every character in a string. Actually
I need to convert the following stored procedure of SQL Server to MySQL. I
I need to grab the path from the registry. The following code works except
If I have a for loop like the following: foreach(string email in installerEmails.Split(',')) {
I have a date I need to store in a date field in mysql.
I only have access to 'C' and need to replace characters within a character
I am trying to insert a time only value, but get the following error
I'm following the example of code available in: http://www.openssl.org/docs/crypto/sha.html# After the following: EVP_DigestFinal_ex(&mdctx, md_value,

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.