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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:33:59+00:00 2026-05-15T07:33:59+00:00

char *myfunc() { char *temp = string; return temp; } In this piece of

  • 0
char *myfunc() {
    char *temp = "string";
    return temp;
}

In this piece of code, where does the allocation of the object pointed to by temp happen and what would be its scope?

Is this function a valid way to return a char* pointer?

  • 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-15T07:34:00+00:00Added an answer on May 15, 2026 at 7:34 am

    Is the code correct?

    Yes your code is (almost) fine, because "string" is a string literal and located in static storage.

    Note: A pointer is just a variable which stores a memory address. This line simply stores the address of the string literal “string” inside a variable called temp.

    char *temp = "string";
    

    The C++ standard guarantees that the string literal will stay in memory for the duration of the program as defined below. Which means you are free to use that memory address in any scope anywhere during the whole life of your program.


    Why?

    The C++03 standard (current) has this to say:

    An ordinary string literal has type
    “array of n const char” and static
    storage duration (3.7),

    And section 3.7.1 – 1:

    All objects which neither have dynamic
    storage duration nor are local have
    static storage duration. The storage
    for these objects shall last for the
    duration of the program.


    Warning:

    In your code you are returning a char*, you should really be returning a const char *. It is undefined behavior if you try to modify a string literal, and your function return value shouldn’t pretend to allow it.

    On a related side note to the warning. If you have in your code in 2 different places a string called "string" then whether or not they are distinct strings is implementation defined.

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

Sidebar

Ask A Question

Stats

  • Questions 496k
  • Answers 496k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The sql ro do this is simply: select * from… May 16, 2026 at 11:41 am
  • Editorial Team
    Editorial Team added an answer A session, in web terms, is a bunch of data… May 16, 2026 at 11:41 am
  • Editorial Team
    Editorial Team added an answer Firstly the sample xml is not really xml as the… May 16, 2026 at 11:41 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

What is the type of &a in the following code? char a[100]; myfunc(&a) Is
Here is my code: void reverseStr(char *str) { if (str == NULL) return; int
How to find the size of a predefined char[] variable that holds a string?
can I do something like: typedef void (*functor)(void* param); //machine code of function char
Here is my function's code: char * increment_b_string(char * b_string) { char * ret_string
I wrote the following code: void buildArrays(char *pLastLetter[],int length[], int size, const char str[])
I have a unmanaged C++ function with the following signature: int function(char* param, int
I am using borland 2006 c++, and have following code. I am using vectors,
The C function myfunc operates on a larger chunk of data. The results are
I assumed that std::wstring and std::string both provide more or less the same interface.

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.