Chapter 10 Pointers
Chapter 10 Pointers
Introduction
A pointer is a variable that represents the location (rather
than the value) of a data item, such as a variable or an array
element.
Pointers are used frequently in C, as they have a number of
useful applications.
For example, pointers can be used to pass information back and
forth between a function and its reference point.
In particular, pointers provide a way to return multiple data
items from a function via function arguments.
Pointers also permit references to other functions to be specified
as arguments to a given function. This has the effect of passing
functions as arguments to the given function.
Pointers are also closely associated with arrays and therefore
provide an alternate way to access individual array elements.
Moreover, pointers provide a convenient way to represent
multidimensional arrays, allowing a single multidimensional
array to be replaced by a lower-dimensional array of pointers.
This feature permits a group of strings to be represented within
a single array, though the individual strings may differ in length.
FUNDAMENTALS
Suppose v is a variable that represents some particular
data items. The compiler will automatically assign memory
cells for this data item.
The data item can then be accessed if we know the
location (i.e., the address) of the first memory cell.
The address of v ’s memory location can be determined by
the expression &v, where & is a unary operator, called the
address operator, that evaluates the address of its
operand.
Now let us assign the address of v to another variable, pv.
Thus, pv = &v
This new variable is called a pointer to v, since it “points”
to the location where v is stored in memory.
Remember, however, that pv represents v’s address, not
its value. Thus, pv is referred to as a pointer variable.
The relationship between pv and v is illustrated
FUNDAMENTALS
The data item represented by v (i.e., the data item
stored in v’s memory cells) can be accessed by the
expression *pv, where * is a unary operator, called
the indirection operator, that operates only on a
pointer.
Output:
POINTERS AND ONE-
DIMENSIONAL ARRAYS
On the other hand, it is sometimes necessary to assign
an address to an identifier.
In such situations, a pointer variable must appear on the
left side of the assignment statement.
It is not possible to assign an arbitrary address to an
array name or to an array element.
Thus, expressions such as x, ( x + i) and &x [i] cannot
appear on the left side of an assignment statement.
Moreover, the address of an array cannot arbitrarily be
altered, so that expressions such as ++x are not
permitted.
On the other hand, we can assign the value of one
array element to another through a pointer if we wish,
e.g., pl = &line[l];
line[2] = *pl;
POINTERS AND ONE-
DIMENSIONAL ARRAYS
Strings can be assigned to pointer variables rather
than to one dimensional arrays.
ALLOCATION
Since an array name is actually a pointer to the first
element within the array, it should be possible to
define the array as a pointer variable rather than as a
conventional array.
Syntactically, the two definitions are equivalent.
However, a conventional array definition results in a
fixed block of memory being reserved at the
beginning of program execution, whereas this does
not occur if the array is represented in terms of a
pointer variable.
Therefore, the use of a pointer variable to represent
an array requires some type of initial memory
assignment before the array elements are processed.
This is known as dynamic memory allocation.
Generally, the malloc library function is used for this
ALLOCATION
ALLOCATION
An important advantage of dynamic memory
allocation is the ability to reserve as much memory
as may be required during program execution, and
then release this memory when it is no longer
needed.
Moreover, this process may be repeated many
times during execution of a program. The library
functions malloc and free are used for these
purposes.
POINTERS
Suppose, for example, that px is a pointer variable
that represents the address of some variable x.
We can write expressions such as ++px, - - px, (px
+ 3 ) , (px + i),and (px - i),where i is an integer
variable.
Each expression will represent an address that is
located some distance from the original address
represented by px.
POINTERS
POINTERS
The operations that can be carried out on pointers are
summarized below-
1. A pointer variable can be assigned the address of an
ordinary variable (e.g., pv = &v).
2. A pointer variable can be assigned the value of another
pointer variable (e.g., pv = px) provided both pointers
point to objects of the same data type .
3. A pointer variable can be assigned a null (zero) value
(e.g., pv = NULL, where NULL is a symbolic constant that
represents the value 0).
4. An integer quantity can be added to or subtracted from
a pointer variable (e.g., pv + 3, ++pv, etc.)
5. One pointer variable can be subtracted from another
provided both pointers point to elements of the same array.
6. Two pointer variables can be compared provided both
pointers point to objects of the same data type.
POINTERS AND
MULTIDIMENSIONAL ARRAYS
We can define a two-dimensional array as a pointer to a
group of
contiguous one-dimensional arrays.
Thus, a two-dimensional array declaration can be written as
data- type ( *ptvar) [ expression 2] ;
rather than
data- type array[ expression I] [ expression 2];
This concept can be generalized to higher-dimensional
arrays; that is,
data- type ( *ptvar)[ expression 21 [ expression 31 . . . [
expression n] ;
replaces
data- type array[ expression 1)[ expression 21 . .
[ expression n] ;
See EXAMPLE 10.22 Adding Two Tables of Numbers.
ARRAYS OF POINTERS
A multidimensional array can be expressed in terms of an array of
pointers rather than a pointer to a group of contiguous arrays.
In such situations the newly defined array will have one less
dimension than the original multidimensional array. Each pointer
will indicate the beginning of a separate (n- 1)-dimensional array.
In general terms, a two-dimensional array can be defined as a one-
dimensional array of pointers by writing-
data - type *array[ expression 7 ) ;
rather than the conventional array definition,
data- type array[ expression 7 ] [ expression 21 ;
Similarly, an n-dimensional array can be defined as an (n - 1)-
dimensional array of pointers by writing-
data- type *array[ expression 71 [ expression 2) . . .
[ expression n- 71 ;
rather than
data- type array[ expression I ] [ expression 21 . . .
[ expression n] ;
PASSING FUNCTIONS TO OTHER
FUNCTIONS
A pointer to a function can be passed to another
function as an argument.
This allows one function to be transferred to
another, as though the first function were a
variable.
Let us refer to the first function as the guest
function, and the second function as the host
function. Thus, the guest is passed to the host,
where it can be accessed.
Successive calls to the host function can pass
different pointers (i.e., different guest functions) to
the host.
Thank You!!!