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

  • Home
  • SEARCH
  • 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 6921975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:20:46+00:00 2026-05-27T10:20:46+00:00

I recently read about scope rules in C. It says that a local or

  • 0

I recently read about scope rules in C. It says that a local or auto variable is available only inside the block of the function in which it is declared. Once outside the function it no longer is visible. Also that its lifetime is only till the end of the final closing braces of the function body.

Now here is the problem. What happens when the address of a local variable is returned from the function to the calling function ?

For example :-

 main()
 {
     int *p=fun();
 }

 int * fun()
 { 
     int  localvar=0;
     return (&localvar);
 }

once the control returns back from the function fun, the variable localvar is no longer alive. So how will main be able to access the contents at this address ?

  • 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-27T10:20:46+00:00Added an answer on May 27, 2026 at 10:20 am

    The address can be returned, but the value stored at the address cannot reliably be read. Indeed, it is not even clear that you can safely assign it, though the chances are that on most machines there wouldn’t be a problem with that.

    You can often read the address, but the behaviour is undefined (read ‘bad: to be avoided at all costs!‘). In particular, the address may be used for other variables in other functions, so if you access it after calling other functions, you are definitely unlikely to see the last value stored in the variable by the function that returned the pointer to it.


    Why then is a function returning a pointer ever required?

    One reason is often ‘dynamic memory’. The malloc() family of functions return a pointer to new (non-stack) memory.

    Another reason is ‘found something at this location in a value passed to me’. Consider strchr() or strstr().

    Another reason is ‘returning pointer to a static object, either hidden in the function or in the file containing the source for the function’. Consider asctime() et al (and worry about thread-safety).

    There are probably a few others, but those are probably the most common.

    Note that none of these return a pointer to a local (stack-based) variable.

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

Sidebar

Related Questions

I thought I read about a C standard library function recently that was able
Recently i read an article is about prevent brute-force attack. It said that automatically
I recently read these notes about SEO which say that you loose Google juice
I recently read about http://php.net/pcntl and was woundering how good that functions works and
I've recently read a lot about software (mostly scientific/math and encryption related) that moves
I recently read about a new Google-code hosted (open source) project from Google that
Quite recently I read about JavaScript call usage in MDC https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/call one linke of
I recently read about the fact that there is a possibility of defining getters/setters
I recently read an article that talked about the computation complexity of algorithms. The
I recently read a question on here about static and dynamic linking, which reminded

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.