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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:56:52+00:00 2026-05-30T06:56:52+00:00

Here is the code, char *foo() { static char s[10] = abcde; return s;

  • 0

Here is the code,

char *foo()
{
    static char s[10] = "abcde";
    return s;
}

char *bar()
{
    char *c = foo();
    return c;
}

int main()
{
    printf("%s\n", bar()); 
}

Typically, it is wrong to return a local pointer as I did in bar, but now c points to a static var returned by foo, would it be correct to return a local var c in bar?

I tried, it printf the right value, but I don’t understand how it works. I thought, when bar() finishes, the var c should vanish, which should make printf print undefined stuff, right?

Follow Up

char *c is a local var, if char *c = "abcde";, I assume this: c is a local var which resides in the function’s stack, while "abcde" is a constant var which resides in the constants-area (part of the heap?), so when bar() finishes, c vanishes, but "abcde" still remains in the heap, right?

  • 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-30T06:56:53+00:00Added an answer on May 30, 2026 at 6:56 am

    Variable c is only a pointer. It is not wrong to return a local pointer from a function, you do it all the time. For example, when you store a result of malloc in a pointer, the pointer is local, but the storage it points to is not. It is, however, wrong to return a pointer to a local storage. Since in your example c never points to a locally allocated data, your code works correctly as written.

    EDIT (in response to the Follow Up)

    “abcde” is a constant var which resides in the constants-area (part of the heap?)

    The constants area is not usually part of the heap, it is either a separate arrea, usually adjacent to the area where the machine code of your program is stored.

    c vanishes, but “abcde” still remains in the heap, right?

    “abcde” remains in the constants area, not in the heap, but the concept is correct: the pointer to that constant remains valid throughout the entire run-time of your program.

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

Sidebar

Related Questions

Here's the code snippet: public static void main (String[]arg) { char ca = 'a'
Here is my code: class Foo { public: Foo(const char*); }; class Bar {
I’ve got the following code: #include <iostream> using namespace std; int main() { char*
When I run the following code: #include <stdio.h> int main(int argc, char *argv[]) {
where is the mistake? My code here: typedef struct _box { char *dados; struct
I have a piece of C code that looks like this: const char (*foo)[2]
I receive a pointer numeric value like: 0xbfe0e6ac it is possible set int foo
Ok the error is showing up somewhere in this here code if($error==false) { $query
Here is code from MSDN . I don't understand why the work isn't just
Here a code to demonstrate an annoying problem: class A { public: A(): m_b(1),

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.