WHAT IS A POINTER? - PowerPoint PPT Presentation

About This Presentation
Title:

WHAT IS A POINTER?

Description:

WHAT IS A POINTER – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 27
Provided by: robert1776
Category:
Tags: pointer | what | refers | to | what

less

Transcript and Presenter's Notes

Title: WHAT IS A POINTER?


1
WHAT IS A POINTER?
  • Simply stated, a pointer is an address.
  • A running program consists of three parts
    execution stack, code, and data. They are stored
    in main memory.
  • Each cell of the main memory has an address. A
    pointer variable is a variable whose value is
    either NULL (0) or a legal address.

2
Why pointers?
  • Pointers are used in programs to access memory
    and manipulate addresses.
  • To get the effect of call-by-reference in C, we
    must use pointers in the parameter list of a
    function definition and pass addresses of
    variables as arguments in the function call.
  • To hold the memory location of a chunk of
    dynamically allocated memory block.

3
How to declare pointers
  • A pointer must be defined to point to some type
    of variable.
  • Following a proper definition, it cannot be used
    to point to any other type of variable or it will
    result in a "type incompatibility" error.
  • Declaration examples
  • int p, pp // how to read?
  • char cp
  • float fp1, fp2, fp3

4
Example
  • int k 567, p
  • p k
  • // p refers to k
  • // p points to k
  • // p contains the address of k

567
p
k
5
How to initialize pointers?
  • A pointer variable must be initialized to an
    address of something before you can access the
    value located at that address.
  • Two operators
  • address operator
  • dereference operator

6
Example
  • int x 5, y 6, z
  • int px, py pz
  • px x
  • py y
  • pz z
  • pz px py // z x y (11)
  • y 2 px y // y ?

7
Example
  • // the dereference operator is the
  • // inverse of the address operator
  • double x, y, p
  • p x
  • y p // y x or y x
  • int a, p a // OK
  • int p a, a // wrong

8
Exercises
  • int i 3, j 5, p i, q j, r
  • double x

Expression Equivalence
p i p (i)
p i 7 p (i 7)
p ((p))
r x r (x)
7 p/q7 (((7(p)))/(q))7
(r j)p ((r (j))) (p)
Value






Value
1
illegal
3
illegal
11
15
9
Pointers to void
  • void is a generic pointer type (in ANSI C).
  • int x 6, px x
  • void vp
  • double qx
  • qx px // illegal
  • vp px // ok
  • qx vp // ok
  • qx (double) px // ok

10
Call by Reference vs Call by Value
  • When variables are passed as arguments to a
    function, their values are copied to the
    corresponding function parameters, and variables
    themselves are not changed in the calling
    environment. This Call-by-Value mechanism is
    strictly enforced in C.
  • For a function to effect Call-by-Reference,
    pointers must be used in the parameter list of
    the function definition. Then, when the function
    is called, addresses of variables must be passed
    as arguments.

11
Example
Before swap
  • void swap(int p, int q)
  • int tmp
  • tmp p
  • p q
  • q tmp
  • int main(void)
  • int a 3, b 7
  • swap(a, b)

p
a 3
q
b 7
After swap
a 7
p
b 3
q
12
Example return multi-values through pointers
  • void minmax(int table, int n, int minp, int
    maxp)
  • int i, min INT_MIN, max INT_MAX
  • for (i 0 i lt n i)
  • if (tablei lt min) min tablei
  • else if (tablei gt max) max tablei
  • minp min
  • maxp max
  • int main()
  • int table 10, 15, 38, 27, -21, 88, 69
  • int min, max
  • minmax(table, 7, min, max)
  • // print min and max

13
Array and Pointers
  • An array name by itself is a base address, or
    pointer value, and can be thought of as a
    constant pointer.
  • The base address of an array is the initial
    location in memory where the array is stored,
    that is, it is the address of the first element
    (index 0) of the array.

14
Relationship between Arrays and Pointers
  • define N 100
  • int aN, pa, sum, i
  • // suppose array a has been initialized
  • pa a
  • pa a0
  • pa a 10
  • pa a10
  • sum 0
  • for ( pa a pa lt aN pa) sum pa
  • sum 0
  • for ( i 0 i lt N i) sum ai

equivalent
equivalent
15
Example return multi-values through an array
  • void minmax(int table, int n, int results)
  • int i, min INT_MIN, max INT_MAX
  • for (i 0 i lt n i)
  • if (tablei lt min) min tablei
  • else if (tablei gt max) max tablei
  • results0 min
  • results1 max
  • int main()
  • int table 10, 15, 38, 27, -21, 88, 69
  • int results2
  • minmax(table, 7, results)
  • // print results0 and result1

16
Passing arrays to functions
  • int sum(int a, int n)
  • int j, s 0
  • for (j 0 j lt n j) s aj
  • return s

Calls What gets computed
sum(v, 100) v0 v1 v99
sum(v, 88) v0 v1 v87
sum(v7, k-7) v7 v8 vk-1
sum(v 7, 2k) v7 v8 v2k6
17
Pointer Arithmetic
  • Pointer arithmetic is automatically done in
    units of the pointers underlying base type.
  • If p is a pointer to a particular type, then
  • p 1 yields the correct machine address for
    storing or accessing the next variable of that
    type.
  • In a similar fashion, p i, p, p i all make
    sense.
  • If p and q are both pointing to elements of an
    array, then p q yields the integer value
    representing the number of elements between p and
    q.

18
Pointer Arithmetic
  • The only legal arithmetic operators on pointers
    are adding or subtracting an integer, or
    subtracting one pointer from another.
  • Pointers can be compared ( ! lt lt gt gt
    ). Two pointers are equal only if they point to
    the same location. One pointer is less than
    another if it points a lower location in memory.
  • Dont compare pointers that dont access the
    same array.

19
Another Example
  • Suppose we want to implement a statistics
    program for an array of data. Functions to be
    implemented are
  • double maximum(double a, int n)
  • double average(double a, int n)
  • double sum(double a, int n)

Problem inefficient code because each function
involves a for loop.
20
Efficient code - using pointers
  • void stat(double a, int n, double p_ave,
  • double p_max, double p_sum)
  • int i
  • p_max p_sum a0
  • for (i 1 i lt n i)
  • p_sum ai
  • if (p_max lt ai) p_max ai
  • p_ave p_sum/(double) n

21
Pointer as return value
int search(int a, int n, int key) int p
a, ep a n while (p lt ep p ! key)
p return p ! ep ? p NULL
  • int foo() // A common mistake
  • int a 5
  • return a

22
Pointers and Efficiency
  • int a, sum, i, p, ep // assume a is
    initialized
  • // run the following schemes 10000 times
  • // scheme 1 ? 20 Sec
  • sum 0
  • for (i 0 i lt 10000 i) sum ai
  • // scheme 2 ? 13 Sec (35 speed up)
  • sum 0 p a
  • for (i 0 i lt 10000 i) sum p
  • // scheme 3 ? 12 Sec (40 speed up)
  • sum 0 p a ep a 10000
  • while (p lt ep) sum p

23
Generic Pointers
  • // suppose we want to copy one array into
    another
  • double f10, g10
  • f g // illegal, f is a constant address
  • memcpy(f, g, sizeof(g)) // call library
    function
  • -------------------------------------------------
    ---------------
  • void memcpy(void xp, const void yp, size_t
    n)
  • unsigned char dp xp
  • const unsigned char sp yp
  • const unsigned char ep yp n
  • while (sp lt ep) dp sp
  • return xp

24
Dynamical Memory Allocation
  • C requires the number of items in an array to be
    known at compile time. Too big or too small?
  • Dynamical memory allocation allow us to specify
    an arrays size at run time.
  • Two important library functions are malloc,
    which allocates space from HEAP, and free, which
    returns the space (allocated by malloc) back to
    HEAP for reuse later.

25
Example
  • / allocate and free an array of doubles, with
    error check /
  • include ltstdio.hgt
  • include ltstddef.hgt // definition of NULL
  • include ltstdlib.hgt // definition for
    malloc/free
  • double dcreate(int n)
  • double dp
  • if ((dp malloc(nsizeof(double)) ! NULL)
  • return dp
  • printf(dcreate dynamic allocation failed.)
  • exit(0)
  • void dfree(double dp)
  • if (dp ! NULL) free(dp)

26
Some Comments
  • Dont assume malloc will always succeed.
  • Dont assume the dynamically allocated memory is
    initialized to zero.
  • Dont modify the pointer returned by malloc.
  • free only pointers obtained from malloc, and
    dont access the memory after it has been freed.
  • Dont forget to free memory which is no longer
    in use (garbage).
Write a Comment
User Comments (0)
About PowerShow.com