0% found this document useful (0 votes)
3 views

Chapter 10 Pointers

Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Chapter 10 Pointers

Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 32

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.

Furthermore, if we write pv = &v and U = *pv, then


U and v will both represent the same value; i.e., the
value of v will indirectly be assigned to U. (It is
assumed that U and v are of the same data type.)
Example
Shown below is a simple program that illustrates
the relationship between two integer variables,
their corresponding addresses and their
associated pointers.
Example
The relationships between pu and u, and pv and v,
are shown in the following figure. Note that the
memory locations of the pointer variables (i.e.,
address EC7 for pu, and EC5 for pv) are not
displayed by the program.
FUNDAMENTALS
The address operator (&) must act upon operands
that are associated with unique addresses, such
as ordinary variables or single array elements.
Thus the address operator cannot act upon
arithmetic expressions, such as 2 * (U + v).
The indirection operator (*) can only act upon
operands that are pointers (e.g., pointer
variables).
However, if pv points to v (i.e., pv = &v), then
an expression such as *pv can be used
interchangeably with its corresponding variable v.
Thus, an indirect reference (e.g., *pv) can appear
in place of an ordinary variable (e.g., v) within a
more complicated expression.
Example
POINTER DECLARATIONS
The variable name must be preceded by an asterisk
(*).
This identifies the fact that the variable is a pointer.
The data type that appears in the declaration refers
to the object of the pointer, i.e., the data item that
is stored in the address represented by the pointer,
rather than the pointer itself.
Thus, a pointer declaration may be written in
general terms as
data- type *ptvar;
where ptvar is the name of the pointer variable, and
data-type refers to the data type of the pointer’s
object. Remember that an asterisk must precede
ptvar.
FUNCTION
Pointers are often passed to a function as
arguments.
We refer to this use of pointers as passing
arguments by reference (or by address or by
location),in contrast to passing arguments by
value.
Thus, the use of a pointer as a function
argument permits the corresponding data item to
be altered globally from within the function.
Example
Here is a simple C program that illustrates the
difference between ordinary arguments, which are
passed by value, and pointer arguments, which
are passed by reference.
Analyzing a Line of Text
Let us write a complete C program that will carry out an
analysis on a line of text to count the no of vowels,
consonants, digits, whitespace characters and “other”
characters (punctuation, operators, brackets, etc.).
To do so, we first define the following symbols.
line = an 80-element character array containing the line of
text
vowels = an integer counter indicating the number of
vowels
consonants = an integer counter indicating the number of
consonants
digits = an integer counter indicating the number of digits
whitespc = an integer counter indicating the number of
whitespace characters (blank spaces or tabs)
other = an integer counter indicating the number of
characters that do not fall into any of the preceding
Input and Output
Contd…
It is possible to pass a portion of an array, rather
than an entire array, to a function. To do so, the
address of the first array element to be passed
must be specified as an argument. The remainder
of the array, starting with the specified array
element, will then be passed to the function.
POINTERS AND ONE-
DIMENSIONAL ARRAYS
Recall that an array name is really a pointer to the
first element in the array.
Therefore, if x is a one dimensional array, then
the address of the first array element can be
expressed as either &x [ 0] or simply as x.
Moreover, the address of the second array
element can be written as either &x [ 1 ] or as (x
+ 1), and so on.
POINTERS AND ONE-DIMENSIONAL
ARRAYS
POINTERS AND ONE-
DIMENSIONAL ARRAYS

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!!!

You might also like