views:

2309

answers:

5

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

+2  A: 

See http://stackoverflow.com/questions/496448/how-to-correctly-use-the-extern-keword-in-c

And http://stackoverflow.com/questions/534735/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).

Michael
+2  A: 

short answer. Static is persistent so if you decl it in a func, when you call the func again the value is the same as it was last time. If decl globally, it is only global in that file. Extern means it decl globally but in a dif file. (it basically means this variable does exist and this is what its defined as).

acidzombie24
+3  A: 

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;
   ...
}
anon
+2  A: 

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.

Charles Bailey
That's answering a surprising interpretation of the question. I would expect the question to mean: what are the uses of static pointers, and what are the uses of extern pointers (since I know that both exist, I would ignore that qualification).
Jonathan Leffler
+1  A: 

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;
}
John Bode