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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:26:49+00:00 2026-05-18T05:26:49+00:00

I am a C++ noob and I wanna know how can i return an

  • 0

I am a C++ noob and I wanna know how can i return an array from a C++ function.
I tried the following code but doesn’t seem to work.

char some_function(){
    char my_string[] = "The quick brown fox jumps over the lazy dog";
    return my_string;
}
  • 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-18T05:26:50+00:00Added an answer on May 18, 2026 at 5:26 am

    The reason that code isn’t working is that the minute the function ends, so does the lifetime of the string you have created. Instead, if you’re going to be working in C++ use std::string and return that.

    std::string myFunc(){
        return string("hey a new string");
    }
    

    For other arrays use std::vector, std::deque or one of the other STL classes. I’d also point you to look at the STL (standard template library):

    vector<float> myFunc(){
        vector<float> blah;
        blah.push_back(4.5);
        blah.push_back(5.7);
        return blah;
     }
    

    On returning arrays:

    The big problem with pointers and such is object lifetime considerations. Such as the following code:

    int* myFunc(){
        int myInt = 4;
        return &myInt;
    }
    

    what happens here is that when the function exits myInt no longer exists leaving the pointer that was returned to be pointing at some memory address which may or may not hold the value of 4. If you want to return an array using pointers (I really suggest you don’t and use std::vector) it’ll have to look something like:

    int* myFunc(){
        return new int[4];
    }
    

    which uses the new operator.

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

Sidebar

Related Questions

Noob Question I know but can you explain to me how I can install
This is a noob question, but I wanna know why there are different encoding
Noob question here. Please see code below. Where can I read more on this?
Noob question: Consider the following C# code: public IEnumerable<xpto> CalculatedList { get { foreach(var
noob writing, I'm trying to use a Navigation Controller, but the method pushViewController doesn't
I wanna know how can I get a value of a child of a
Noob developing a Magento theme and I can't seem to figure out why Magento
Noob ADO.NET question: Can I do the following? Retrieve a DataTable somehow. Dispose it.
noob here. The following is a fragment from a class definition I came accross
Noob dumb question, no doubt -- but here it is: Trying to tutorialize myself

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.