

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 iinntt'') 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 iinntt-
length object.  Likewise, the declaration


        int **pointer;


declares that  pointer holds the address of  a pointer whose con-
tents, in turn, point to an iinntt-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)(_a_r_g_1, _a_r_g_2);


_H_e_r_e, _t_h_e  `*' _t_a_k_e_s _t_h_e  _c_o_n_t_e_n_t_s _o_f _t_h_e _p_o_i_n_t_e_r,  _w_h_i_c_h _i_n _t_h_i_s
_c_a_s_e _i_s  _t_h_e _a_d_d_r_e_s_s  _o_f _t_h_e _f_u_n_c_t_i_o_n,  _a_n_d _u_s_e_s _t_h_a_t  _a_d_d_r_e_s_s _t_o
_p_a_s_s _t_o _a _f_u_n_c_t_i_o_n _i_t_s _l_i_s_t _o_f _a_r_g_u_m_e_n_t_s.

_A _p_o_i_n_t_e_r _t_o  _a _f_u_n_c_t_i_o_n _c_a_n _b_e _p_a_s_s_e_d _a_s  _a_n _a_r_g_u_m_e_n_t _t_o _a_n_o_t_h_e_r
_f_u_n_c_t_i_o_n.   _T_h_e _f_u_n_c_t_i_o_n_s  _b_s_e_a_r_c_h _a_n_d  _q_s_o_r_t _b_o_t_h  _t_a_k_e _f_u_n_c_t_i_o_n
_p_o_i_n_t_e_r_s  _a_s _a_r_g_u_m_e_n_t_s.   _A  _p_r_o_g_r_a_m _m_a_y  _a_l_s_o _u_s_e  _o_f _a_r_r_a_y_s  _o_f
_p_o_i_n_t_e_r_s _t_o _f_u_n_c_t_i_o_n_s.

***** _P_o_i_n_t_e_r _C_o_n_v_e_r_s_i_o_n *****

_O_n_e _t_y_p_e  _o_f _p_o_i_n_t_e_r _m_a_y _b_e _c_o_n_v_e_r_t_e_d, _o_r  _c_a_s_t, _t_o _a_n_o_t_h_e_r.  _F_o_r
_e_x_a_m_p_l_e, _a _p_o_i_n_t_e_r _t_o _a _c_h_a_r  _m_a_y _b_e _c_a_s_t _t_o _a _p_o_i_n_t_e_r _t_o _a_n _i_n_t,
_a_n_d _v_i_c_e _v_e_r_s_a.

_P_o_i_n_t_e_r_s _t_o  _d_i_f_f_e_r_e_n_t _d_a_t_a _t_y_p_e_s _a_r_e  _c_o_m_p_a_t_i_b_l_e _i_n _e_x_p_r_e_s_s_i_o_n_s,
_b_u_t _o_n_l_y _i_f _t_h_e_y _a_r_e _c_a_s_t _a_p_p_r_o_p_r_i_a_t_e_l_y.  _U_s_i_n_g _t_h_e_m _w_i_t_h_o_u_t _c_a_s-
_t_i_n_g _p_r_o_d_u_c_e_s _a _p_o_i_n_t_e_r-_t_y_p_e _m_i_s_m_a_t_c_h.

***** _P_o_i_n_t_e_r _A_r_i_t_h_m_e_t_i_c *****

_A_r_i_t_h_m_e_t_i_c  _m_a_y _b_e  _p_e_r_f_o_r_m_e_d _o_n  _a_l_l  _p_o_i_n_t_e_r_s _t_o  _s_c_a_l_a_r _t_y_p_e_s,
_i._e.,  _p_o_i_n_t_e_r_s _t_o  cchhaarrs or  iinntt.   Pointer arithmetic  is quite
limited and consists of the following:

11. One pointer may be subtracted from another.

22. An int or a long, either variable or constant, may be added to
   a pointer or subtracted from it.

3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


