C 中的外部指针和静态指针

发布于 2024-08-02 14:25:45 字数 32 浏览 9 评论 0原文

您好,静态和外部指针的用法是什么?如果它们存在的话

Hi what could be the usage of static and extern pointer ?? if they exist

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

究竟谁懂我的在乎 2024-08-09 14:25:46

要回答有关何时可以使用它们的问题,可以使用几个简单的示例:

静态指针可用于实现始终向程序返回相同缓冲区的函数,并在第一次调用时分配它:

char * GetBuffer() {
   static char * buff = 0;
   if ( buff == 0 ) {
       buff = malloc( BUFSIZE );
   }
   return buff;
}

外部(即global) 指针可用于允许其他编译单元访问 main 的参数:

extern int ArgC = 0;
extern char ** ArgV = 0;

int main( int argc, char ** argv ) {
   ArgC = argc;
   ArgV = argv;
   ...
}

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:

char * GetBuffer() {
   static char * buff = 0;
   if ( buff == 0 ) {
       buff = malloc( BUFSIZE );
   }
   return buff;
}

An extern (i.e. global) pointer could be used to allow other compilation units to access the parameters of main:

extern int ArgC = 0;
extern char ** ArgV = 0;

int main( int argc, char ** argv ) {
   ArgC = argc;
   ArgV = argv;
   ...
}
嗼ふ静 2024-08-09 14:25:46

简短的回答:它们不存在。 C99 6.7.1 说“至多,一个存储类说明符可以是在声明中的声明说明符中给出”。 externstatic 都是存储类说明符。

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 and static are both storage class specifiers.

把回忆走一遍 2024-08-09 14:25:46

假设我有一个指针,我想让多个翻译单元全局可用。我想在一个地方(foo.c)定义它,但允许在其他翻译单元中对其进行多个声明。 “extern”关键字告诉编译器这不是对象的定义声明;实际的定义将出现在其他地方。它只是使对象名称可供链接器使用。编译和链接代码时,所有不同的翻译单元都将通过该名称引用同一对象。

假设我还有一个指针,我确实希望使其对单个源文件中的函数全局可用,但不让它对其他翻译单元可见。我可以使用“static”关键字来指示对象的名称不导出到链接器。

假设我还有一个指针,我只想在单个函数中使用该指针,但在函数调用之间保留该指针的值。我可以再次使用“static”关键字来指示该对象具有静态范围;它的内存将在程序启动时分配,直到程序结束才释放,因此该对象的值将在函数调用之间保留。

/** 
 * foo.h
 */
#ifndef FOO_H
#define FOO_H

/**
 * Non-defining declaration of aGlobalPointer; makes the name available
 * to other translation units, but does not allocate the pointer object
 * itself.
 */
extern int *aGlobalPointer;

/**
 * A function that uses all three pointers (extern on a function 
 * declaration serves roughly the same purpose as on a variable declaration)
 */
extern void demoPointers(void);
...
#endif

/**
 * foo.c
 */
#include "foo.h"

/**
 * Defining declaration for aGlobalPointer.  Since the declaration 
 * appears at file scope (outside of any function) it will have static 
 * extent (memory for it will be allocated at program start and released 
 * at program end), and the name will be exported to the linker.
 */
int *aGlobalPointer;

/**
 * Defining declaration for aLocalPointer.  Like aGlobalPointer, it has 
 * static extent, but the presence of the "static" keyword prevents 
 * the name from being exported to the linker.  
 */
static int *aLocalPointer;

void demoPointers(void)
{
  /**
   * The static keyword indicates that aReallyLocalPointer has static extent, 
   * so the memory for it will not be released when the function exits,
   * even though it is not accessible outside of this function definition
   * (at least not by name)
   */
  static int *aReallyLocalPointer;
}

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.

/** 
 * foo.h
 */
#ifndef FOO_H
#define FOO_H

/**
 * Non-defining declaration of aGlobalPointer; makes the name available
 * to other translation units, but does not allocate the pointer object
 * itself.
 */
extern int *aGlobalPointer;

/**
 * A function that uses all three pointers (extern on a function 
 * declaration serves roughly the same purpose as on a variable declaration)
 */
extern void demoPointers(void);
...
#endif

/**
 * foo.c
 */
#include "foo.h"

/**
 * Defining declaration for aGlobalPointer.  Since the declaration 
 * appears at file scope (outside of any function) it will have static 
 * extent (memory for it will be allocated at program start and released 
 * at program end), and the name will be exported to the linker.
 */
int *aGlobalPointer;

/**
 * Defining declaration for aLocalPointer.  Like aGlobalPointer, it has 
 * static extent, but the presence of the "static" keyword prevents 
 * the name from being exported to the linker.  
 */
static int *aLocalPointer;

void demoPointers(void)
{
  /**
   * The static keyword indicates that aReallyLocalPointer has static extent, 
   * so the memory for it will not be released when the function exits,
   * even though it is not accessible outside of this function definition
   * (at least not by name)
   */
  static int *aReallyLocalPointer;
}
半岛未凉 2024-08-09 14:25:46

请参阅如何在 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).

不知所踪 2024-08-09 14:25:46

简短的回答。
静态是持久的,因此如果您在函数中声明它,当您再次调用该函数时,该值与上次相同。如果您将其声明为全局的,则它仅在该文件中是全局的。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文