Chapter 7' C Pointers - PowerPoint PPT Presentation

1 / 49
About This Presentation
Title:

Chapter 7' C Pointers

Description:

A pointer is a variable that contains a memory address as its value ... const char *suit[4] = {'Hearts', 'Diamonds', 'Clubs', 'Spades'}; ???. Write a program to ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 50
Provided by: csieNd
Category:

less

Transcript and Presenter's Notes

Title: Chapter 7' C Pointers


1
Chapter 7. C Pointers
2
7.2 Pointer variable declarations and
initialization
  • A pointer is a variable that contains a memory
    address as its value
  • A variable directly references a value while a
    pointer indirectly references a value

3
7.2 Pointer variable declarations and
initialization
  • The following declaration declares a pointer and
    an integer
  • int countPtr, count
  • The variable countPtr is declared as type int
    (a pointer pointing to an integer value)
  • Pointers can be declared to point to any type of
    data

4
countPtr
count
7
count directly references the value 7 countPtr
indirectly references the value 7
5
7.2 Pointer variable declarations and
initialization
  • A pointer can be initialized as an address, a
    NULL, or a 0. Using 0 is equivalent to using
    NULL. However, NULL is preferred.
  • int ptr1NULL
  • int ptr20
  • int ptr3y / will be explained later /

6
7.3 Pointer operators
  • The operator (address operator) returns the
    address of a variable. For example, the following
    statement assigns the address of y to yPtr
  • yPtry

7
7.3 Pointer operators
  • The following statements results in the following
    figure
  • int y5
  • int yPtry

yPtr
y
5
8
7.3 Pointer operators
  • The operator (dereferencing operator) returns
    the value pointed by a pointer
  • For example (see the figure and statements in the
    previous slide), the following statement will
    output the value 5.
  • printf(d,yPtr)

9
7.3 Pointer operators
  • The operators and are complements of one
    another (see the program in the next slide)
  • Figure 7.5 shows the precedence of the operators
    and

10
(No Transcript)
11
Simple exercise
  • Use pointers to switch the values of two variable

12
7.4 Calling functions by reference
  • In C, functions are generally called by value.
    Nevertheless, pointers can be used to simulate
    calling by reference
  • See the next slide for an example

13
include ltstdio.hgt void aa(int , int) main()
int a10,b10 printf(Before calling aa, ad,
bd\n,a,b) aa(a,b) printf(After calling
aa, ad, bd\n,a,b) void aa(int x, int
y) x 2 y 2 return
what will be outputted by this program?
14
aa
main
x
a
10
b
y
10
10
15
7.4 Calling functions by reference
  • See Figures 7.6 through 7.9 for a more detailed
    explanation

16
(No Transcript)
17
(No Transcript)
18
(No Transcript)
19
(No Transcript)
20
7.4 Calling functions by reference
  • As mentioned in the previous chapter, passing an
    array to a function results in calling by
    reference. Actually, passing an array corresponds
    to passing the address of the first array element
  • When passing an array, the notation and can
    be used interchangeably (see the next slide)

21
void aa(int , int) main() int a10 . .
. aa(a,10) . . . void aa(int x, int y)
. . .
void aa(int , int) main() int a10 . .
. aa(a,10) . . . void aa(int x, int y)
. . .
22
Simple exercise
  • Use pointers to switch the contents of two arrays
    in a function other than main

23
7.6 Bubble sort using call by reference
  • The program in the following slides is a bubble
    sort using call by reference
  • The call by reference occurs in the function
    swap, see these statements
  • int holdelement1Ptr
  • element1Ptrelement2Ptr
  • element2Ptrhold

24
(No Transcript)
25
(No Transcript)
26
7.6 Bubble sort using call by reference
  • The prototype of the function swap is defined
    inside the function bubbleSort because only
    bubbleSort uses swap. This restricts proper call

27
The sizeof operator (pp. 267-269)
  • The sizeof operator returns the number of bytes
    used by a variable, that of a data type, or that
    of a constant
  • See the programs in the following slides for some
    examples

28
(No Transcript)
29
(No Transcript)
30
(No Transcript)
31
7.7 Pointer expressions and pointer arithmetic
  • If a pointer points to an array element, pointer
    expression and arithmetic are meaningful
  • Pointers can be increased, decreased, added by a
    value, subtracted by a value, or subtracted by
    another pointer

32
7.7 Pointer expressions and pointer arithmetic
  • Example
  • int vPtr, v1,2,3,4,5,6,7,8,9,10,i
  • vPtrv / this statement is the same as
    vPtrv0 /
  • for(i0ilt10vPtr,i)
  • printf(d\n,vPtr) / what will be
    outputted? ?
  • The vPtr advances the pointer to the next array
    element. Therefore, vPtr in the above example
    causes the value of vPtr to be increased by
    sizeof(int)

33
v0
v1
v2
v9
vPtr vPtr1 vPtr2
vPtr9
34
7.7 Pointer expressions and pointer arithmetic
  • Example
  • float vPtr, v10
  • vPtrv / suppose that 7000 is assigned to
    vPtr/
  • vPtr2
  • Suppose that sizeof(float) is 4. Then, what value
    will be assigned to vPtr by the statement vPtr2?

35
7.7 Pointer expressions and pointer arithmetic
  • Example
  • float ptr1,ptr2,v10
  • int i
  • ptr1v3/ suppose ptr1 is set 700C/
  • ptr2v1/ suppose ptr2 is set 7004/
  • iptr2-ptr1 / i will be set the number of array
    elements between the two pointers /
  • What will i gets in the last statement above?

36
Simple exercise
  • Use pointer arithmetic to assign the summation of
    two arrays to the third array, element by element

37
7.7 Pointer expressions and pointer arithmetic
  • A pointer can be assigned to another pointer in
    case that they have the same type
  • Cast operators should be used to convert pointer
    type for the assignment between different pointer
    types. For example
  • ptr1(int)ptr2

38
7.7 Pointer expressions and pointer arithmetic
  • void pointer can used in assignment
  • Example
  • void vptr1,vptr2
  • int iptr1
  • float fptr1
  • vptr1vptr2
  • vptr1iptr1
  • fptr1vptr2

39
7.7 Pointer expressions and pointer arithmetic
  • NOTE a void pointer cannot be dereferenced
    because the compiler does not know what is the
    type pointed by the pointer

40
7.8 The relationship between pointers and arrays
  • An array name can be though as a constant pointer
    (pointing to which array element?)
  • Equivalent expressions (suppose that b is an
    integer array and bPtr is an integer pointer)
  • bPtrb bPtrb0
  • b3 (bPtr3)
  • b3 bPtr3
  • b3 (b3)

41
7.8 The relationship between pointers and arrays
  • If a pointer points to an array, the pointer can
    be increased or decreased (what do the increment
    and decrement mean?)
  • On the other hand, array name cannot be increased
    or decreased because it is actually a constant
    pointer. See the next slide for an example

42
int ptr,a101,2,3,4,5 ptra ptr3 /what
does this statement mean?/ printf(d,ptr) a
3 / a compiler error occur here /
43
7.8 The relationship between pointers and arrays
  • The programs in the next slides show the
    reference of an array using different methods

44
(No Transcript)
45
(No Transcript)
46
(No Transcript)
47
7.9 Arrays of pointers
  • The same type of pointer can be structured as an
    array
  • The following declaration results in the figure
    in the next slide
  • const char suit4 Hearts, Diamonds,
    Clubs, Spades

48
(No Transcript)
49
???
  • Write a program to
  • 1. input 10 different integers from the keyboard,
    in which five integers are stored in array a and
    five are in array b, and
  • 2. use a function other than main to place the
    largest five numbers in array a and the others in
    array b. Please use pointers in this function.
  • Due the day after 2 weeks
Write a Comment
User Comments (0)
About PowerShow.com