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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:54:07+00:00 2026-06-13T21:54:07+00:00

This is a very basic question about the scope of a variable suppose. I

  • 0

This is a very basic question about the scope of a variable suppose. I have the fiollowing code:

int main()
{
    int *p;
    p=func();
    printf("%d",*p);
    return 0;
}

int *func()
{
   int i;
   i=5;
   return &i;
}

My question

  • The scope of i is finished in func() but, since I am returning the address of i will I be able to access and print5 in main()?
  • If not, why? does the compiler puts a garbage value in that address space (I don’t think this is done).
  • What actually it means by the scope of a variable is ended ? Also does the memory allocated to i is freed when its scope ends?
  • 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-13T21:54:10+00:00Added an answer on June 13, 2026 at 9:54 pm

    Scope of the variable is the region where it can be accessed.
    Lifetime of the variable is the time till when the variable is guaranteed to exist.

    In your case lifetime of i is within the function not beyond it. It means i is not guaranteed to exist beyond the function. It is not required to and it is Undefined Behavior to access a local variable beyond the function.

    The scope of i is finished in func() but, since I am returning the address of i will I be able to access and print 5 in main()?

    You might, but it is Undefined Behavior. So don’t do it.

    If not, why? does the compiler puts a garbage value in that address space (I don’t think this is done)

    The compiler may put whatever it chooses to in that location, once the function returns the address location is holds an Indeterminate value.

    What actually it means by the scope of a variable is ended ? Also does the memory allocated to i is freed when its scope ends?

    i is a automatic/local variable and all automatic variables are freed once the scope {,} in which they are declared ends. Hence the name automatic.

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

Sidebar

Related Questions

This is probably a very basic question about AD FS 2.0 claim rule design
This is a very basic question...quite embarassing, but here goes: I have a Stopwatch
I have a very basic question about calculating RMSE in an NB classification scenario.
This is probably a very basic R question...and feel a bit bad about asking...but
I have a very basic question about PHP. So, there is index.php that includes
This is a very basic question. I feel kind of silly asking about it
This might be a very basic question, but I am a bit confused about
I have a very basic question about the LocalMon print monitor found at http://msdn.microsoft.com/en-us/library/windows/hardware/ff556478%28v=vs.85%29.aspx
I have a very basic question about how to write to a plist. I
I have very basic question about the use of high performance cluster in our

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.