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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:25:32+00:00 2026-05-28T01:25:32+00:00

Here is my code #include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char

  • 0

Here is my code

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


int main() {

 char f[] = "First";
 char s[] = "Second";
 char *tmp = malloc(strlen(f) + strlen(s) + 2);
 strcpy(tmp, f);
 strcpy(tmp, s);
 printf("%s", tmp);
 free(tmp);
 return 0;
}

I’m trying to concatenate f and s. The problem is that tmp contains only “Second” as a array.
What I miss here

  • 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-28T01:25:33+00:00Added an answer on May 28, 2026 at 1:25 am

    If you insist on using strcpy, your code should be slightly modified:

    int main() {
        const char *f = "First";
        const char *s = "Second";
        char *tmp = malloc(strlen(f) + strlen(s) + 1);
        strcpy(tmp, f);
        strcpy(tmp+strlen(f), s);
        printf("%s", tmp);
        free(tmp);
        return 0;
    }
    

    You should consider using strncpy instead of strcpy for safety reasons. Also, strcat is a more conventional function for concatenating C string.

    EDIT Here is an example of using strncpy instead of strcpy

    #define MAX 1024
    
    int main() {
        const char *f = "First";
        const char *s = "Second";
        size_t len_f = min(strlen(f), MAX);
        size_t len_s = min(strlen(s), MAX);
        size_t len_total = len_f + len_s;
        char *tmp = malloc(len_total + 1);
        strncpy(tmp, f, len_f);
        strncpy(tmp+len_f, s, len_s);
        tmp[len_total] = '\0';
        printf("%s", tmp);
        free(tmp);
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The code is here: #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char*
Here is my code: #include <stdio.h> int main(void) { FILE *fp; unsigned int i;
Here's my code so far: #include<iostream> #include<string> #include<fstream> using namespace std; int main() {
Here is my code: #include <stdio.h> #include <stdlib.h> #include <dirent.h> #include <string.h> #include <sys/stat.h>
Have a look at the following code: #include<unistd.h> #include<stdlib.h> #include<stdio.h> #include<string.h> #include<sys/types.h> main() {
Here is my code: #include <iostream> #include <stdlib.h> #include <fstream> using namespace std; int
here is code which prints longest string duplicated M times #include <iostream> #include <stdlib.h>
I have a code: #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <string.h> typedef struct
I am going to paste here my code and errors: #include stdio.h #include winsock2.h
Here is my code which checks if the file exists : #include<stdio.h> #include<zlib.h> #include<unistd.h>

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.