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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:11:36+00:00 2026-06-11T21:11:36+00:00

I am new to cpp and have a question regarding arrays. The code I

  • 0

I am new to cpp and have a question regarding arrays. The code I have below should create a reversed version of str and have it be stored in newStr. However, newStr always comes up empty. Can someone explain to me why this is happening even though I am assigning a value from str into it?

void reverse (char* str) {
    char* newStr = (char*)malloc(sizeof(str));

    for (int i=0;i<sizeof(str)/sizeof(char);i++) {
       int index = sizeof(str)/sizeof(char)-1-i;
       newStr [i] = str [index];
    }
}

PS: I know that it is much more efficient to reverse an array by moving the pointer or by using the std::reverse function but I am interested in why the above code does not work.

  • 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-11T21:11:37+00:00Added an answer on June 11, 2026 at 9:11 pm

    As above commenters pointed out sizeof(str) does not tell you the length of the string. You should use size_t len = strlen(str);

    void reverse (char* str) {
       size_t len = strlen(str);
       char* newStr = (char*)malloc(len + 1);
    
       for (int i=0; i<len;i++) {
          int index = len-1-i;
          newStr[i] = str[index];
       }
       newStr[len] = '\0'; // Add terminator to the new string.
    }
    

    Don’t forget to free any memory you malloc. I assume your function is going to return your new string?

    Edit: +1 on the length to make room for the terminator.

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

Sidebar

Related Questions

New programmer here, I am trying to understand and break down this code below
I have a question about the source-code binary on Windows. #include <stdio.h> int main()
Being fairly new to C++ I have a question bascially concerning the g++ compiler
i have some question. I try this code and recieve Segmentation fault error: #include
I am new in C++ and I have a basic question for you. It
I have written some code in Lib_file.h and Lib_file.cpp. I wish to convert this
I have a small question about threading in Windows. I have the following code
I do not have much experience with C++ and I have a question regarding
i have a question regarding qt in c++ header file: #ifndef TIMER_H #define TIMER_H
I have a question about Qt. I am wondering how it is I should

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.