Pointers - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Pointers

Description:

the operator & in front of an ordinary variable produces the address of that variable. ... array is stored in sequential memory locations, in row order. ... – PowerPoint PPT presentation

Number of Views:55
Avg rating:3.0/5.0
Slides: 25
Provided by: jeanine4
Category:
Tags: pointers

less

Transcript and Presenter's Notes

Title: Pointers


1
Chapter 5
  • Pointers

2
Addresses and Pointers
3
Pointers
  • Fundamental part of C.
  • Secret to C is in its use of pointers.
  • C uses pointers a lot. Why?
  • Only way to express some computations.
  • Produces compact and efficient code.
  • Provides a very powerful tool.
  • C uses pointers explicitly with
  • Arrays,
  • Structures,
  • Functions.
  • Pointers are perhaps the most difficult part of C
    to understand.

4
Address Operator
  • A variable can be referenced using the address
    operator
  • example
  • scanf(f, x)
  • This statement specifies that the value read is
    to be stored at the address of x

5
Pointer Assignment
  • A pointer is a variable that holds the address of
    a memory location
  • If a variable p holds the address of another
    variable q, then p is said to point to q
  • If q is a variable at location 100 in memory,
    then p would have the value 100 (qs address)

6
How to declare a pointer variable
  • pointer variables are declared using an asterisk
    ( ) The asterisk is called the indirection
    operator or the de-referencing operator).
  • example
  • int a, b, ptr
  • ptr is a pointer to an integer
  • when a pointer is defined, the type of variable
    to which it will point must be specified. (i.e.
    a pointer defined to point to an integer cannot
    also point to a floating point variable.)

7
Example
  • int iPtr
  • double dPtr
  • the variable iPtr is declared to point to an int
  • the variable dPtr is declared to point to a
    double
  • neither variable has been initialized in the
    above example
  • declaring a pointer creates a variable capable of
    holding an address

8
More about declaring pointers
  • When using the form
  • Int p, q
  • the operator does not distribute.
  • In the above example p is declared to be a
    pointer to int. q is declared to be an int.

9
Assigning values to a pointer
  • the assignment operator () is defined for
    pointers
  • the right operand can be any expression that
    evaluates to the same type as the left
  • the operator in front of an ordinary variable
    produces the address of that variable. The
    operator is called to address of operator

10
Example
  • example -
  • int I6, j
  • int iPtr
  • iPtr I
  • j iPtr
  • I iPtr
  • j

6
6
11
Practice!
Give a memory snapshot after each set of
assignment statements int a1, b2, ptr ptr
b int a1, b2, ptrb a ptr
12
NULL pointer
  • A pointer can be assigned or compared to the
    integer zero, or, equivalently, to the symbolic
    constant NULL, which is defined in ltstdio.hgt.
  • A pointer variable whose value is NULL is not
    pointing to anything that can be accessed

13
Practice
  • int x 1, y 2
  • int ip
  • ip x
  • y ip  
  • x ip  
  • ip 3
  • What is the final value of x, y, ip ?

14
Pointer Assignments
  • A pointer can point to only one location at a
    time, but several pointers can point to the same
    location.
  • Example
  • / Declare and initialize variables. /
  • int x-5, y 8, ptr1, ptr2
  • / Assign both pointers to point to x. /
  • ptr1 x
  • ptr2 ptr1
  • The memory snapshot after these statements are
    executed is
  • ptr1 ptr2
  • x y

-5
8
15
Practice
  • Write a function to swap variables around.

16
First Attempt
  • include ltstdio.hgt
  • void main (void)
  • float a 3, b 6
  • void swap(float a, float b)
  • swap(a,b)
  • printf(f f\n, a, b)
  • return
  • void swap(float a, float b)
  • float temp
  • temp a
  • a b
  • b temp
  • return

What is the output?
17
Practice
  • The usual function call
  •     swap(a, b)   WON'T WORK.
  • Pointers provide the solution Pass the address
    of the variables to the functions and access
    address of function.
  • Thus our function call in our program would look
    like this
  •     swap(a, b)

18
Swap function
  • The Code to swap is fairly straightforward
  • void swap(int px, int py)
  • int temp
  • temp px / contents of pointer /
  • px py
  • py temp

19
Pointer Arithmetic
  • Four arithmetic operations are supported
  • , -, , --
  • only integers may be used in these operations
  • Arithmetic is performed relative to the variable
    type being pointed to
  • Example p
  • if p is defined as int p, p will be incremented
    by 4 (system dependent)
  • if p is defined as double p, p will be
    incremented by 8(system dependent
  • when applied to pointers, means increment
    pointer to point to next value in memory

20
Comparing Pointers
  • You may compare pointers using relational
    operators
  • Common comparisons are
  • check for null pointer (p NULL)
  • check if two pointers are pointing to the same
    object
  • (p q) Is this equivalent to ?
  • (p q)
  • compare two pointers that are pointing to a
    common object such as an array.

21
Two-Dimensional Arrays
  • A two-dimensional array is stored in sequential
    memory locations, in row order.

Array definition int s23 2,4,6,
1,5,3, sptrs00

//correction Memory allocation s00 2
s01 4 s02 6 s10 1 s11 5 s1
2 3 A pointer reference to s01 would be
(sptr1) A pointer reference to s11 would be
(sptr4) row offset number of columns
column offset
22
Pointers in Function References
  • In C, function references are call-by-value
    except when an array name is is used as an
    argument.
  • An array name is the address of the first element
  • Values in an array can be modified by statements
    within a function
  • To modify a function argument, a pointer to the
    argument must be passed
  • The actual parameter that corresponds to a
    pointer argument must be an address or pointer.

23
switch Example
void switch2(int a, int b) / Declare
Variables. / int temp / Switch values
pointed to by a and b. / temp
a ab btemp / Void return.
/ return
24
Common Pointer Problems
  • Using un-initialized pointers
  • int iPtr
  • iPtr 100
  • iPtr has not been initialized. The value 100
    will be assigned to some memory location,
    resulting in various types of errors.
  • Failing to reset a pointer after changing its
    value
  • Incorrect/unintended syntax
Write a Comment
User Comments (0)
About PowerShow.com