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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:56:19+00:00 2026-06-01T03:56:19+00:00

Wouldn’t the pointer returned by the following function be inaccessible? char *foo(int rc) {

  • 0

Wouldn’t the pointer returned by the following function be inaccessible?

char *foo(int rc)
{
    switch (rc)
    {
        case 1:

            return("one");

        case 2:

            return("two");

        default:

            return("whatever");
    }
}

So the lifetime of a local variable in C/C++ is practically only within the function, right? Which means, after char* foo(int) terminates, the pointer it returns no longer means anything, right?

I’m a bit confused about the lifetime of a local variable. What is a good clarification?

  • 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-01T03:56:20+00:00Added an answer on June 1, 2026 at 3:56 am

    Yes, the lifetime of a local variable is within the scope({,}) in which it is created.

    Local variables have automatic or local storage. Automatic because they are automatically destroyed once the scope within which they are created ends.

    However, What you have here is a string literal, which is allocated in an implementation-defined read-only memory. String literals are different from local variables and they remain alive throughout the program lifetime. They have static duration [Ref 1] lifetime.

    A word of caution!

    However, note that any attempt to modify the contents of a string literal is an undefined behavior (UB). User programs are not allowed to modify the contents of a string literal.
    Hence, it is always encouraged to use a const while declaring a string literal.

    const char*p = "string"; 
    

    instead of,

    char*p = "string";    
    

    In fact, in C++ it is deprecated to declare a string literal without the const though not in C. However, declaring a string literal with a const gives you the advantage that compilers would usually give you a warning in case you attempt to modify the string literal in the second case.

    Sample program:

    #include<string.h> 
    int main() 
    { 
        char *str1 = "string Literal"; 
        const char *str2 = "string Literal"; 
        char source[]="Sample string"; 
     
        strcpy(str1,source);    // No warning or error just Undefined Behavior 
        strcpy(str2,source);    // Compiler issues a warning 
     
        return 0; 
    } 
    

    Output:

    cc1: warnings being treated as errors
    prog.c: In function ‘main’:
    prog.c:9: error: passing argument 1 of ‘strcpy’ discards qualifiers from pointer target type

    Notice the compiler warns for the second case, but not for the first.


    To answer the question being asked by a couple of users here:

    What is the deal with integral literals?

    In other words, is the following code valid?

    int *foo()
    {
        return &(2);
    } 
    

    The answer is, no this code is not valid. It is ill-formed and will give a compiler error.

    Something like:

    prog.c:3: error: lvalue required as unary ‘&’ operand
         
    

    String literals are l-values, i.e: You can take the address of a string literal, but cannot change its contents.
    However, any other literals (int, float, char, etc.) are r-values (the C standard uses the term the value of an expression for these) and their address cannot be taken at all.


    [Ref 1]C99 standard 6.4.5/5 "String Literals – Semantics":

    In translation phase 7, a byte or code of value zero is appended to each multibyte character sequence that results from a string literal or literals. The multibyte character sequence is then used to initialize an array of static storage duration and length just sufficient to contain the sequence. For character string literals, the array elements have type char, and are initialized with the individual bytes of the multibyte character sequence; for wide string literals, the array elements have type wchar_t, and are initialized with the sequence of wide characters…

    It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.

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

Sidebar

Related Questions

Why wouldn't the following line of code work in a method? return (count >
Why wouldn't it just work? const char* ip = 192.168.1.4; unsigned int ip4[4]; sscanf(ip,%o.%o.%o.%o,ip4,ip4+1,ip4+2,ip4+3);
Would you please explain why the following shell command wouldn't work: sh-3.1$ echo $MYPATH
I was a bit surprised when the following code wouldn't compile: -- Code 1
I wouldn't be suprised if this turned out to be a BIDS function rather
I wouldn't mind writing my own function to do this but I was wondering
Can anyone tell my why this wouldn't compile? package main type myint int func
Wouldn't this be overkill and only one of these necessary? I've searched and found
Following snippet wouldn't compile. With following error: Cannot implicitly convert type 'Container<ChildClass>' to 'Container<BaseClass>'
I'm not sure why the following wouldn't be working. I am getting a blank

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.