8

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

4

5 回答 5

15

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;
   ...
}
于 2009-08-17T07:40:29.270 回答
9

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.

于 2009-08-17T07:45:57.497 回答
3

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;
}
于 2009-08-17T14:48:33.060 回答
2

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

于 2009-08-17T07:06:04.020 回答
2

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

于 2009-08-17T07:21:05.067 回答