A pointer is an object whose value is the address of another object. The name ``pointer'' derives from the fact that its contents ``point to'' another object. A pointer may point to any type, complete or incomplete, including another pointer. It may also point to a function, or to nowhere.
The term pointer type refers to the object of a pointer. The object to which a pointer points is called the referenced type. For example, an int * (``pointer to iinntt'') is a pointer type; the referenced type is int. Constructing a pointer type from a referenced type is called pointer type derivation.
int *pointer;
declares that the variable pointer holds the address of an iinntt-length object. Likewise, the declaration
int **pointer;
declares that pointer holds the address of a pointer whose contents, in turn, point to an iinntt-length object.
Failure to declare a function that returns a pointer will result in that function being implicitly declared as an int. This does not cause an error on microprocessors in which an int and a pointer have the same size; however, if you transport this code to a microprocessor in which an int consists of 16 bits and a pointer consists of 32 bits, the pointer will be truncated truncated to 16 bits and the program probably will fail.
C allows pointers and integers to be compared or converted to each other without restriction. The COHERENT C compiler flags such conversions with the strict message
integer pointer pun
and comparisons with the strict message
integer pointer comparison
These problems should be corrected if you want your code to be portable to other computing environments.
See C language for more information.
When a program declares a pointer, space is set aside in memory for it. However, this space has not yet been filled with the address of an object. To fill a pointer with the address of the object you wish to access is called initializing it. A wild pointer, as often as not, is one that is not properly initialized.
Normally, to initialize a pointer means to fill it with a meaningful address. For example, the following initializes a pointer:
int number;
int *pointer;
. . .
pointer = &number;
The address operator `&' specifies that you want the address of an object rather than its contents. Thus, pointer is filled with the address of number, and it can now be used to access the contents of number.
The initialization of a string is somewhat different than the initialization of a pointer to an integer object. For example,
char *string = "This is a string."
declares that string is a pointer to a char. It then stores the string literal This is a string in memory and fills string with the address of its first character. string can then be passed to functions to access the string, or you can step through the string by incrementing string until its contents point to the null character at the end of the string.
Another way to initialize a pointer is to fill it with a value returned by a function that returns a pointer. For example, the code
extern char *malloc(size_t variable);
char *example;
. . .
example = malloc(50);
uses the function malloc to allocate 50 bytes of dynamic memory and then initializes example to the address that malloc returns.
int number;
int *pointer;
. . .
pointer = &number;
. . .
printf("%d\n", *pointer);
uses pointer to access the contents of number.
When a pointer points to a structure, the elements within the structure can be read by using the structure offset operator `->'. See the entry for operators for more information.
char *(*example)();
declares example to be a pointer to a function that returns a pointer to a char.
This declaration is quite different from:
char **different();
The latter declares that different is a function that returns a pointer to a pointer to a char.
The following demonstrates how to call a function via a pointer:
(*example)(_a_r_g_1, _a_r_g_2);
Here, the `*' takes the contents of the pointer, which in this case is the address of the function, and uses that address to pass to a function its list of arguments.
A pointer to a function can be passed as an argument to another function. The functions bsearch and qsort each take a function pointer as an argument. A program may also use arrays of pointers to functions.
In Kernighan and Ritchie C, character pointers are equivalent to void *. To convert a program to use void *, rewrite the sources so that instances of
char *foo(bar);
is replaced by:
VOID_T *foo(bar);
Be sure that you do not replace legitimate cchhaarr **s -- that is, pointers that actually point to character strings. Then put the code
#if defined(__ANSI__) || defined(__GNUC__)
typedef void VOID_T
#else
typedef char VOID_T
#endif
into an application-owned header file that is included by every source file.
The ANSI Standard" states that any pointer can be cast to type void * and back again without its value being affected in any way. (Once again, please note that COHERENT's C compiler does not yet recognize the type void *.) Likewise, any pointer of a scalar type may be cast to its corresponding const or volatile version. The qualified pointers are equivalent to their unqualified originals.
Pointers to different data types are compatible in expressions, but only if they are cast appropriately. Using them without casting produces a pointer-type mismatch. The translator should produce a diagnostic message when it detects this condition.
When an int or long is added to a pointer, it is first multiplied by the length of what the pointer is declared as pointing to. Thus, if a pointer to an int is incremented by two, it points down two more ints, not two more characters. The following program demonstrates this feature:
char *pc = "Welcome";
int array[5] = { 1, 2, 3, 4, 5 };
int *pi = array;
main()
{
pc += 2; /* pc points to 'l' */
pi += 2; /* pi points to 3 */
}