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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:30:57+00:00 2026-06-07T16:30:57+00:00

The following code only concatenates the first string and ignores the second.. from what

  • 0

The following code only concatenates the first string and ignores the second.. from what I gather, it has something to do with Null terminated strings. As I am new to C, this is a new concept to me. Could someone help make the code below work? That would really help me a lot in understanding this.

void concatTest();

int main()
{
    concatTest();

    system("PAUSE");
    return 0;
}

void concatTest()
{
    char string1[20], string2[20], string3[40];
    char *ptr1, *ptr2, *ptr3;
    ptr1 = &string1[0];
    ptr2 = &string2[0];
    ptr3 = &string3[0];
    int i;

    printf("You need to enter 2 strings.. each of which is no more than 20 chars in length: \n");

    printf("Enter string #1: \n");
    scanf("%s", string1);

    printf("Enter string #2: \n");
    scanf("%s", string2);

    int len1 = strlen(string1);
    int len2 = strlen(string2);

    for (i = 0; i < len1; i++)
    {
        ptr3[i] = ptr1[i];
    }
    for (i = len1; i < len1 + len2; i++)
    {
        ptr3[i] = ptr2[i];
    }

    printf("%s\n", string3);
}
  • 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-07T16:30:58+00:00Added an answer on June 7, 2026 at 4:30 pm

    You are indexing ptr2[i] using i which ranges from len1 to len1 + len2. This value will probably be out of bounds of the string2 array (unless the first string you type happens to be empty).

    I might write your second loop as follows:

    for (i = 0; i < len2; i++) {
        ptr3[len1 + i] = ptr2[i];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I run the following code, it only seems to be downloading the first
for example, the following code only run the task in 1 process: from jug
Does anybody know why the following code only has effect in FF? $(document).ready(function() {
Can anyone help me decipher why the following code only works on the first
I have the following code (only sections of it) for getting the entries in
What should I do to marshal an hash of arrays? The following code only
One recommends me the following code apparently only in .zshrc without explaining its purpose
I have multiple messages in SQS. The following code always returns only one, even
I have the following code which will allow only numbers 0-9. But i want
When I compile the following code, I only see the error during Run-time which

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.