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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T08:16:40+00:00 2026-06-16T08:16:40+00:00

Just start learning C++ and trying to make a simple function that do substring

  • 0

Just start learning C++ and trying to make a simple function that do substring with following code:

char* substring(int start, int end, const char* target)
{
    size_t length = strlen(target);

    char result[(length + 1)];

    int iterator = 0;

    for(int i = start; i < length; i++)
    {
        if(i <= end)
        {
            result[iterator] = target[i];

            iterator++;
        }
    }

    result[iterator] = '\0';

    cout << result << endl; // This give correct result "aya";

    return result;
}

When I use these function on main method:

int main(int argc, char** argv) {

    char some_text[] = "Saya Makan";

    char* sub = substring(1, 3, some_text); // No string returned

    cout << "Substring Result is: " << sub;

    return 0;
}

It give output is like this:

aya
Substring Result is: 
RUN SUCCESSFUL (total time: 44ms)

As seen on the result, my function isn’t returning anything but empty string although a line of code before return, the “result” variable echoed result correctly. Would you please explain to me what is happening inside my function, am I missing something?

  • 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-16T08:16:41+00:00Added an answer on June 16, 2026 at 8:16 am

    You are returning pointer to a local array which is not required to live beyond the function.

    You need to make the buffer persist beyond the scope of the function by allocating it dynamically using malloc. And ofcourse remember to free it later.

    Oh just noticed its C++.
    So drop the idea of malloc and free simply use std::string and not char *.

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

Sidebar

Related Questions

As part of learning ELisp, I am trying to make a function that makes
I just start learning WPF, is that possible I can create an IE toolbar
I wrote simple class that on the start it just increase the value of
I just started learning about Kinect through some quick start videos and was trying
I'm trying to make a simple, fluid, responsive two column layout (just for the
I just start learning to write web services yesterday, kinda fascinating. I created an
I just start learning Python + Tornado for my web servers. Every time I
Just start to learning Cocos2d and getting errors in Xcode 4.3.3 (4E3002). Problem in
I just installed the latest SDK from Apple so I can start learning iPhone
I just start playing with GWT I'm having a really hard time to make

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.