Extern and Static Pointers in C

55,294

Solution 1

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.

Solution 2

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

Solution 3

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

Share:
55,294
Arpit
Author by

Arpit

Updated on July 09, 2022

Comments

  • Arpit
    Arpit almost 2 years

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

  • Jonathan Leffler
    Jonathan Leffler almost 15 years
    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).