External and static pointers in C

Hi, what could be the use of static and external pointer ?? if they exist

+7
c pointers static extern
source share
5 answers

To answer the question of when they can be used, a few simple examples:

A static pointer can be used to implement a function that always returns the same buffer to the program, allocating it on the first call:

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

An external pointer (i.e. global) can be used to allow other compilation units to access main parameters:

 extern int ArgC = 0; extern char ** ArgV = 0; int main( int argc, char ** argv ) { ArgC = argc; ArgV = argv; ... } 
+10
source share

Short answer: they do not exist. C99 6.7.1 states: "A declaration may specify one storage class qualifier in the declaration qualifiers." extern and static are both storage class specifiers.

+7
source share

See How to use the extern keyword in C

And internal static variables in C, would you use them?

Essentially, β€œstatic” (in the C standard) when used in a function allows you not to destroy the variable, as usual, after the function ends (that is, it retains its old value each time the function is called). "Extern" extends the scope of the variable, so it can be used in other files (ie. It makes a global variable).

+2
source share

short answer. Static is persistent, so if you declare it in a function when you call the function again, the value will be the same as last time. If you declare it globally, it will be global only in this file.

Extern means it is declared globally, but in a different file. (this basically means that this variable exists, and that it is defined as).

+2
source share

Suppose I have a pointer that I want to make globally available for 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 a defining declaration for an 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 different translation units will refer to the same object under this name.

Suppose I also have a pointer that I want to make globally accessible for functions in one source file, but not have visible translation units. I can use the keyword "static" to indicate that the name of the object will not be exported to the layout.

Suppose I also have a pointer that I want to use only inside one function, but that has a pointer value between function calls. I can again use the keyword "static" to indicate that the object has a static degree; memory for it will be allocated when the program starts and will not be released until the program ends, so the value of the object will be saved 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; } 
+2
source share

All Articles