Chapter 8 Pointers - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Chapter 8 Pointers

Description:

Chapter 8 Pointers – PowerPoint PPT presentation

Number of Views:41
Avg rating:3.0/5.0
Slides: 19
Provided by: yanj
Category:

less

Transcript and Presenter's Notes

Title: Chapter 8 Pointers


1
Chapter 8Pointers
  • C, How to Program
  • Deitel Deitel

2
Pointer Variable Declarations
  • Pointer variables contain memory addresses as
    values
  • Normally, variable contains specific value
    (direct reference)
  • Pointers contain address of variable that has
    specific value (indirect reference)
  • Example
  • int countPtr //a pointer to int
  • int count //int variable
  • //pointer variables to double
  • double xPtr, yPtr

3
Variable and Pointer Variable
  • Indirection
  • Referencing value through pointer

4
Pointer Variable Initialization (1)
  • Initialized to 0, NULL
  • 0 or NULL points to nothing (null pointer)
  • 0 is the only integer value that can be assigned
    directly to a pointer variable without casting
    the integer to a pointer type first.
  • Example
  • int countPtr
  • countPtr 0

5
Pointer Variable Initialization (2)
  • Initialized to an address
  • Using the address operator ()
  • Example
  • int y 5 //int variable y
  • int yPtr //declare a pointer yPtr to int
  • yPtr y //and assign the address of y to
    yPtr.

6
Pointer Operators
  • Indirection/Dereferenceing operator()
  • Returns synonym for the object its operand points
    to
  • yPtr returns y (because yPtr points to y)
  • yptr 9 -gt y 9
  • An attempt to dereference a variable that is not
    a pointer is a compilation error.
  • and are inverses of each other
  • y -gt y
  • yPtr -gt yPtr

7
Pointer Types
2000
12 x 3000 2000 ptr
  • int x
  • x 12
  • int ptr
  • ptr x
  • stdcout ltlt ptr
  • ptr is the value in the place to which ptr
    points

8
Pointer Types
2000 12
5 x 3000 2000 ptr
  • int x
  • x 12
  • int ptr
  • ptr x
  • ptr 5
  • // changes the value
  • // at adddress ptr to 5

9
Pointer Types
  • char ch
  • ch A
  • char q
  • q ch
  • q Z
  • char p
  • p q
  • // the right side has value 4000
  • // now p and q both point to ch

4000 A
Z ch 5000
6000 4000 4000 q
p
10
Pointers and Arrays (1)
  • Arrays and pointers are closely related
  • Array name is like constant pointer
  • The value saved in an array name is the address
    of the first element of the array.
  • We can use pointer as the array name.

11
Pointers and Arrays (2)
  • Accessing array elements with pointers
  • Assume declarations
  • int b 5 int bPtr bPtr b
  • Element b n can be accessed by ( bPtr n )
  • Called pointer/offset notation
  • Addresses
  • b 3 is same as bPtr 3
  • Array name can be treated as pointer
  • b 3 is same as ( b 3 )
  • Pointers can be subscripted (pointer/subscript
    notation)
  • bPtr 3 is same as b 3

12
Pointer Arithmetic (1)
  • Pointer arithmetic
  • Increment/decrement pointer ( or --)
  • Add/subtract an integer to/from a pointer ( or
    ,- or -)
  • Pointers may be subtracted from each other
  • Pointer arithmetic is meaningless unless
    performed on a pointer to an array

13
Pointer Assignment
  • Pointer can be assigned to another pointer if
    both are of same type
  • Example
  • int area 1
  • double pArea area //wrong

14
Passing Arguments to Functions by Reference with
Pointers
  • Three ways to pass arguments to a function
  • Pass-by-value
  • Pass-by-reference with reference arguments
  • Pass-by-reference with pointer arguments
  • A function can return only zero or one value
    explicitly.
  • Arguments passed to a function using reference
    arguments
  • Function can modify original values of arguments
  • More than one value returned implicitly.

15
Pass-by-reference With Pointer Arguments
  • Simulates pass-by-reference
  • Use pointers and indirection operator
  • Pass address of argument using operator
  • operator used as alias/nickname for variable
    inside of function
  • Example
  • void squareByReference(int ) //function
    prototype
  • //function declaration
  • void squareByReference(int nPtr)
  • nPtr nPtr nPtr
  • int x
  • squareByReference(x) //call this function

16
sizeof operator (1)
  • Returns size of operand in bytes
  • For arrays, sizeof returns
  • ( size of 1 element ) ( number of elements )
  • If sizeof( int ) returns 4 then int myArray
    10 cout ltlt sizeof( myArray )
  • will print 40
  • Can be used with
  • Variable names
  • Type names
  • Constant values

17
sizeof operator (2)
  • Using the sizeof operator in a function to find
    the size in bytes of an array parameter results
    in the size in bytes of a pointer, not the size
    in bytes of the array.
  • Is performed at compiler-time
  • For double realArray 22
  • Use sizeof realArray / sizeof( double ) to
    calculate the number of elements in realArray
  • Parentheses are only required if the operand is a
    type name

18
Reference
  • Reproduced from the Cyber Classroom for C, How
    to Program, 5/e by Deitel Deitel.
  • Reproduced by permission of Pearson Education,
    Inc.
Write a Comment
User Comments (0)
About PowerShow.com