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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:08:58+00:00 2026-06-01T01:08:58+00:00

I have to make a function which concatenates two strings but I have to

  • 0

I have to make a function which concatenates two strings but I have to add a ‘\n’ after the first word. I figured everything out and for some reason it doesn’t print out anything. Any ideas? It probably has to do something with the pointers. I just can’t get my head around them. Here’s the code.

char *function(char *s1, char *s2){
    char *newStr;
    int size;
    size = strlen(s1) + strlen(s2);

    newStr = (char *)malloc((size+1)*sizeof(char));

    while(*s1!= '\0'){
        *newStr = *s1;
        newStr++;
        s1++;
    }
    *newStr = '\n';
    newStr++;
    while(*s2 != '\0'){
        *newStr = *s2;
        newStr++;
        s2++;
    }
    *newStr = '\0';
return newStr;
}

int main (int argc, const char * argv[]) {
    char *str1 = "Hello";
    char *str2 = "World";

    printf("%s",function(str1, str2));

     return 0;
}

So as a result I should get:

Hello
World

but I’m not getting anything back.

  • 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-01T01:09:00+00:00Added an answer on June 1, 2026 at 1:09 am

    You are returning a pointer to the end of the buffer rather than a pointer to the start of the buffer. Look at the last two lines of the function:

    *newStr = '\0';
    return newStr;
    

    Clearly this returns a pointer to the null char, i.e. the empty string.

    Solve the problem by introducing a temporary pointer which you will use to step through the output buffer. Then you can return the pointer to the beginning of the output buffer.

    char *function(char *s1, char *s2){
        int size = strlen(s1) + strlen(s2) + 2;//one for '\n', one for '\0'
        char *result = malloc(size);
        char *p = result;
    
        while(*s1 != '\0'){
            *p = *s1;
            p++;
            s1++;
        }
        *p = '\n';
        p++;
        while(*s2 != '\0'){
            *p = *s2;
            p++;
            s2++;
        }
        *p = '\0';
    
        return result;
    }
    

    You also need to allocate an extra char for the \n, as shown above. Finally, your calling code never frees the memory allocated by function.

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

Sidebar

Related Questions

Say, i have a function which returns a reference and i want to make
I am having a classes which have function to make random text and captcha
I have: void add_all_msgs(std::deque<Message>::iterator &iter); How can I make that function generic, so it
I have a function: char *make_text(void) { char txt[MAXLEN]; //make something return txt; }
I have a javascript function that calls a generic function to make an ajax
I have link that calls a function when clicked: <a href=javascript:spawnMenu(this); id=link1>Test1</a> To make
I have to write my own hash function. If I wanted to just make
gcc 4.4.2 c89 I have a function that has to run (config_relays). It make
I edited the question so it would make more sense. I have a function
Scenario: I have a Question which has 2 radio buttons & two subforms: subformA

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.