简体   繁体   中英

access Pointers in C programming

I'm new to programming, and am trying to get a better understanding of pointers specifically. Could a static variable be declared inside a function and then accessed or deferenced from outside the function? Why would you not allow explicit pointers to it out of scope, since the memory of the static variable would remain allocated.

Could a static variable be declared inside a function and then accessed or deferenced from outside the function?

The language allows it. So, yes, you can access a pointer to it from outside the function and dereference the pointer from outside the function.

Why would you not allow explicit pointers to it out of scope, since the memory of the static variable would remain allocated.

There are risks associated with that. A calling function can change the state of the static variable. That may or may not be OK depending on the overall structure of your program.

Could a static variable be declared inside a function and then accessed or deferenced from outside the function?

Yes, you can do that.

Explain the scenario where you need this, but in general I don't think its a good approach. Just declare it global.

That question was part of a Homework in Programming 101 when I was in college :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM