pointer C Language pointer
A pointer is an object whose value is the address of another ob-
ject. The name ``pointer'' derives from the fact that its con-
tents ``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 ob-
ject to which a pointer points is called the referenced type.
For example, an int * (``pointer to int'') is a pointer type; the
referenced type is int. Constructing a pointer type from a
referenced type is called pointer type derivation.
***** The Null Pointer *****
A pointer that points to nowhere is a null pointer. The macro
NULL, which is defined in the header stdio.h, defines the null
pointer. The null pointer is an integer constant with the value
zero. It compares unequal to a pointer to any object or func-
tion.
***** Declaring a Pointer *****
To declare a pointer, use the indirection operator `*'. For ex-
ample, the declaration
int *pointer;
declares that the variable pointer holds the address of an int-
length object. Likewise, the declaration
int **pointer;
declares that pointer holds the address of a pointer whose con-
tents, in turn, point to an int-length object.
Failure to declare a function that returns a pointer will result
in that function being implicitly declared as an int. This will
not cause an error on microprocessors in which an int and a
pointer have the same size; however, transporting this code to a
microprocessor in which an int consists of 16 bits and a pointer
consists of 32 bits will result in the pointers being truncated
to 16 bits and the program probably failing.
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
COHERENT Lexicon Page 1
pointer C Language pointer
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 declarations for more information.
***** Wild Pointers *****
Pointers are omnipresent in C. C also allows you to use a
pointer to read or write the object to which the pointer points;
this is called pointer dereferencing. Because a pointer can
point to any place within memory, it is possible to write C code
that generates unpredictable results, corrupts itself, or even
obliterates the operating system if running in unprotected mode.
A pointer that aims where it ought not is called a wild pointer.
When a program declares a pointer, space is set aside in memory
for it. However, this space has not yet been filled with the ad-
dress 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 in-
itialized.
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 con-
tents of number.
The initialization of a string is somewhat different than the in-
itialization of a pointer to an integer object. For example,
COHERENT Lexicon Page 2
pointer C Language pointer
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.
***** Reading What a Pointer Points To *****
The indirection operator `*' can be used to read the object to
which a pointer points. For example,
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 -> for more information.
***** Pointers to Functions *****
A pointer can also contain the address of a function. For ex-
ample,
char *(*example)();
declares example to be a pointer to a function that returns a
pointer to a char.
COHERENT Lexicon Page 3
pointer C Language pointer
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)(arg1, arg2);
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 both take function
pointers as arguments. A program may also use of arrays of
pointers to functions.
***** Pointer Conversion *****
One type of pointer may be converted, or cast, to another. For
example, a pointer to a char may be cast to a pointer to an int,
and vice versa.
Pointers to different data types are compatible in expressions,
but only if they are cast appropriately. Using them without cas-
ting produces a pointer-type mismatch.
***** Pointer Arithmetic *****
Arithmetic may be performed on all pointers to scalar types,
i.e., pointers to chars or int. Pointer arithmetic is quite
limited and consists of the following:
1. One pointer may be subtracted from another.
2. An int or a long, either variable or constant, may be added to
a pointer or subtracted from it.
3. The operators ++ or -- may be used to increment or decrement a
pointer.
No other pointer arithmetic is permitted. No arithmetic can be
performed on pointers to non-scalar objects, e.g., pointers to
functions.
COHERENT Lexicon Page 4
pointer C Language pointer
***** i8086 Pointers *****
Intel designed the i8086 to use a segmented architecture. This
means that the i8086 divides memory into 64-kilobyte segments.
To program the i8086 requires that you use a specific memory
model, which describes how the segments of memory are to be or-
ganized.
***** See Also *****
C language, data formats, operators, portability
COHERENT Lexicon Page 5