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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:42:38+00:00 2026-05-11T20:42:38+00:00

This question is just for my better understanding of static variables in C++. I

  • 0

This question is just for my better understanding of static variables in C++.

I thought I could return a reference to a local variable in C++ if it was declared static since the variable should live-on after the function returns. Why doesn’t this work?

#include <stdio.h>
char* illegal()
{
  char * word = "hello" ;
  return word ;
}

char* alsoNotLegal()
{
  static char * word = "why am I not legal?" ;
  return word ;
}


int main()
{
  // I know this is illegal
  //char * ill = illegal();
  //ill[ 0 ] = '5' ;
  //puts( ill ) ;

  // but why is this? I thought the static variable should "live on" forever -
  char * leg = alsoNotLegal() ;
  leg[ 0 ] = '5' ;
  puts( leg ) ;
}
  • 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-11T20:42:38+00:00Added an answer on May 11, 2026 at 8:42 pm

    The two functions are not itself illegal. First, you in both case return a copy of a pointer, which points to an object having static storage duration: The string literal will live, during the whole program duration.

    But your main function is all about undefined behavior. You are not allowed to write into a string literal’s memory 🙂 What your main function does can be cut down to equivalent behavior

    "hello"[0] = '5';
    "why am I not legal?"[0] = '5';
    

    Both are undefined behavior and on some platforms crash (good!).

    Edit: Note that string literals have a const type in C++ (not so in C): char const[N]. Your assignment to a pointer to a non-const character triggers a deprecated conversion (which a good implementation will warn about, anyway). Because the above writings to that const array won’t trigger that conversion, the code will mis-compile. Really, your code is doing this

    ((char*)"hello")[0] = '5';
    ((char*)"why am I not legal?")[0] = '5';
    

    Read C++ strings: [] vs *

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

Sidebar

Related Questions

This question is just out of interest, and perhaps could be useful for my
This is just for a better understanding of the ASP.NET framework. When you use
I have made a JSFiddle just for better understanding of my question! So no
This is just a small question, more aimed at understanding the usage of arrays
Please do not mark it as a dupe of this question just yet: Bold
This question is just for a sake of knowledge. I don't have any practical
This question is related to another question I just posted . I'm prepping for
I know I've seen the answer somewhere to this question but I just can't
I have just asked this question an hour ago but with regards to IE8
I just posted this question jQuery - passing arrays in post request , where

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.