|
|
coherent
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 iinntt'') 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 iinntt-
length object. Likewise, the declaration
int **pointer;
declares that pointer holds the address of a pointer whose con-
tents, 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 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 cchhaarrs or iinntt. Pointer arithmetic is quite
limited and consists of the following:
11. One pointer may be subtracted from another.
22. An int or a long, either variable or constant, may be added to
a pointer or subtracted from it.
33. 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
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.