C 中的外部指针和静态指针
您好,静态和外部指针的用法是什么?如果它们存在的话
Hi what could be the usage of static and extern pointer ?? if they exist
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
您好,静态和外部指针的用法是什么?如果它们存在的话
Hi what could be the usage of static and extern pointer ?? if they exist
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
要回答有关何时可以使用它们的问题,可以使用几个简单的示例:
静态指针可用于实现始终向程序返回相同缓冲区的函数,并在第一次调用时分配它:
外部(即global) 指针可用于允许其他编译单元访问 main 的参数:
To answer your question about when they could be used, a couple of simple examples:
A static pointer could be used to implement a function that always returns the same buffer to the program, allocating it the first time it is called:
An extern (i.e. global) pointer could be used to allow other compilation units to access the parameters of main:
简短的回答:它们不存在。 C99 6.7.1 说“至多,一个存储类说明符可以是在声明中的声明说明符中给出”。
extern
和static
都是存储类说明符。Short answer: they don't exist. C99 6.7.1 says "At most, one storage-class specifier may be given in the declaration specifiers in a declaration".
extern
andstatic
are both storage class specifiers.假设我有一个指针,我想让多个翻译单元全局可用。我想在一个地方(foo.c)定义它,但允许在其他翻译单元中对其进行多个声明。 “extern”关键字告诉编译器这不是对象的定义声明;实际的定义将出现在其他地方。它只是使对象名称可供链接器使用。编译和链接代码时,所有不同的翻译单元都将通过该名称引用同一对象。
假设我还有一个指针,我确实希望使其对单个源文件中的函数全局可用,但不让它对其他翻译单元可见。我可以使用“static”关键字来指示对象的名称不导出到链接器。
假设我还有一个指针,我只想在单个函数中使用该指针,但在函数调用之间保留该指针的值。我可以再次使用“static”关键字来指示该对象具有静态范围;它的内存将在程序启动时分配,直到程序结束才释放,因此该对象的值将在函数调用之间保留。
Suppose I have a pointer that I want to make globally available to multiple translation units. I want to define it in one place (foo.c), but allow multiple declarations for it in other translation units. The "extern" keyword tells the compiler that this is not the defining declaration for the object; the actual definition will appear elsewhere. It just makes the object name available to the linker. When the code is compiled and linked, all of the different translation units will be referencing the same object by that name.
Suppose that I also have a pointer that I do want to make globally available to functions within a single source file, but not have it visible to other translation units. I can use the "static" keyword to indicate that the name of the object not be exported to the linker.
Suppose that I also have a pointer that I want to use only within a single function, but have that pointer's value preserved between function calls. I can again use the "static" keyword to indicate that the object has static extent; memory for it will be allocated at program startup and not released until the program ends, so the object's value will be preserved between function calls.
请参阅如何在 C 中正确使用 extern 关键字
C 中的内部静态变量,你会使用它们吗?
本质上,当在函数中使用“静态”(在标准 C 中)时,变量不会像通常在函数结束时那样被擦除(即,每次调用函数时它都保留其旧值)。 “Extern”扩展了变量的范围,以便可以在其他文件中使用它(即,它使其成为全局变量)。
See How to correctly use the extern keyword in C
And Internal static variables in C, would you use them?
Essentially, "static" (in standard C) when used in a function allows a variable not to be wiped out as it normally would once the function ends (i.e., it retains its old value each time the function is called). "Extern" expands the scope of a variable so it can be used in other files (i.e., it makes it a global variable).
简短的回答。
静态是持久的,因此如果您在函数中声明它,当您再次调用该函数时,该值与上次相同。如果您将其声明为全局的,则它仅在该文件中是全局的。
Extern 意味着它在全局范围内声明,但在不同的文件中。 (这基本上意味着这个变量确实存在,这就是它的定义)。
short answer.
Static is persistent so if you declare it in a function, when you call the function again the value is the same as it was last time. If you declare it globally, it is only global in that file.
Extern means it declared globally but in a different file. (it basically means this variable does exist and this is what its defined as).