COHERENT manpages
This page displays the COHERENT manpage for pointer [C Language].
List of available manpages
Index
pointer -- C Language
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 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 function.
Declaring a Pointer
To declare a pointer, use the indirection operator `*'. For example, 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 contents, 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 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.
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 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.
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
operators for more information.
Pointers to Functions
A pointer can also contain the address of a function. For example,
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)(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 each take a function pointer as an
argument. A program may also use arrays of pointers to functions.
void *
void * is the generic pointer; it replaces char * in that role. A pointer
may be cast to void * and then back to its original type without any change
in its value. void * is also aligned for any type in the execution
environment. Please note that COHERENT's C compiler does not yet recognize
the type void *.
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 char *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.
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.
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.
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.
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 */
}
See Also
C language
data formats
operators,
portability,
Programming COHERENT
ANSI Standard, §6.1.2.5, §6.2.2.1, §6.2.2.3, §6.3.2.2-
3, §6.5.4.1









