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

The Archive Base Latest Questions

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

I am learning about C pointers by creating various simple functions. I have just

  • 0

I am learning about C pointers by creating various simple functions. I have just created a function to reverse a char array. It works, but after the output it also displays a bunch of garbage chars (see screenshot below).

Screenshot

Here’s my code:

void reverseString();

int main()
{
    reverseString();

    system("PAUSE");
    return 0;
}

void reverseString()
{
    char string1[20], string2[20];
    char *ptr1, *ptr2;
    ptr1 = &string1[0];
    ptr2 = &string2[0];

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

    int len1 = strlen(string1);

    int i;
    for (i = 0; i < len1; i++)
    {
        ptr2[i] = ptr1[len1 - i - 1];
    }

    printf("%s\n", string2);
}

How can I get rid of the garbage chars? Is there something wrong with my code or did I just nto account for something or what?

  • 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-07T15:30:16+00:00Added an answer on June 7, 2026 at 3:30 pm

    You forgot to nul-terminate the new string:

    int i;
    for (i = 0; i < len1; i++)
    {
        ptr2[i] = ptr1[len1 - i - 1];
    }
    
    //  Add this
    ptr2[i] = '\0';
    

    When you print a char*, it will keep reading until it finds that nul character. But since you left it out, it kept going and going…

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

Sidebar

Related Questions

I am learning C programming and I have a simple question about pointers... I
Very confused over this. I've just started learning about pointers and have now decided
I am just now learning about function pointers and, as I was reading the
I'm learning about creating custom routes in ASP.NET MVC and have hit a brick
I am learning about smart pointers ( std::auto_ptr ) and just read here and
I just started learning about pointers in C++, and I'm not very sure on
I am very new to C and I have some problems learning about pointers.
I learning to use Pointers. I have a few questions about an exercise code
I'm learning about pointers. Given this code: FILE* from = fopen("in.txt", "r"); FILE* to
I'm currently learning about pointers in my C++ book (Programming: Principles and Practice using

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.