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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:59:26+00:00 2026-05-26T23:59:26+00:00

how can i print a char array such i initialize and then concatenate to

  • 0

how can i print a char array such i initialize and then concatenate to another char array? Please see code below

int main () {
char dest[1020];
char source[7]="baby";
cout <<"source: " <<source <<endl;
cout <<"return value: "<<strcat(dest, source) <<endl;
cout << "pointer pass: "<<dest <<endl;
return 0;
}

this is the output

source: baby
return value: v����baby
pointer pass: v����baby

basically i would like to see the output print

source: baby
return value: baby
pointer pass: baby
  • 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-26T23:59:27+00:00Added an answer on May 26, 2026 at 11:59 pm

    You haven’t initialized dest

    char dest[1020] = ""; //should fix it
    

    You were just lucky that it so happened that the 6th (random) value in dest was 0. If it was the 1000th character, your return value would be much longer. If it were greater than 1024 then you’d get undefined behavior.

    Strings as char arrays must be delimited with 0. Otherwise there’s no telling where they end. You could alternatively say that the string ends at its zeroth character by explicitly setting it to 0;

    char dest[1020];
    dest[0] = 0;
    

    Or you could initialize your whole array with 0’s

    char dest[1024] = {};
    

    And since your question is tagged C++ I cannot but note that in C++ we use std::strings which save you from a lot of headache. Operator + can be used to concatenate two std::strings

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

Sidebar

Related Questions

I know you can print with printf() and puts() . I can also see
The JavaScript code window.print() can print the current HTML page. If I have a
private static char[] quicksort (char[] array , int left , int right) { if
The following code triggers C4345 on the marked line: #include <array> #include <iostream> int
If I declare a vector as such: vector<char *> charStarVec(4); and then add values
Please, look at the following code that just convert an unsigned int to a
{ char *a, *b; printf(%lx\n,(b-a)); } Usually works, in fact, I can't imagine it
I can print out a date nicely based on the user's locale. But how
how i can print from ppc (windows mobile) to usb printer that connect to
Is there any way I can print/show images on top of each other. The

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.