我是编程新手,正在尝试更好地理解具体的指针。静态变量可以在函数内部声明,然后从函数外部访问或引用吗?为什么不允许显式指针超出范围,因为静态变量的内存将保持分配状态。
最佳答案
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.
与此相关的风险。调用函数可以更改 static
变量的状态。根据您的程序的整体结构,这可能会或可能不会。
关于c - C 编程中的访问指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32598417/