C Chap07
C Chap07
Outline
7.1 Introduction
7.2 Pointer Variable Declarations and Initialization
7.3 Pointer Operators
7.4 Calling Functions by Reference
7.5 Using the const Qualifier with Pointers
7.6 Bubble Sort Using Call by Reference
7.7 Pointer Expressions and Pointer Arithmetic
7.8 The Relationship between Pointers and Arrays
7.9 Arrays of Pointers
7.10 Case Study: A Card Shuffling and Dealing
Simulation
7.11 Pointers to Functions
y yptr y
5 500000 600000 600000 5
yPtr
Address of y
is value of
yptr
3
Using the & and * operators */
#include <stdio.h>
Outline
4
9 2 Initialize
10 a = 7; variables
The * operator returns an
11 aPtr = &a; /* aPtr set to address of a */
alias to what its operand
12
points to. aPtr points to a,
13 printf( "The address of a is %p" 3. Print
14 "\nThe value of aPtr is %p", &a, aPtr );
so *aPtr returns a.
15
18
23 return 0;
24 }
The value of a is 7
The value of *aPtr is 7 Program Output
Proving that * and & are complements of each other.
&*aPtr = 0012FF88
*&aPtr = 0012FF88
2000 Prentice Hall, Inc.
All rights reserved.
7.4 Calling Functions by Reference
• Call by reference with pointer arguments
– Pass address of argument using & operator
– Allows you to change actual location in memory
– Arrays are not passed with & because the array name is
already a pointer
• * operator
– Used as alias/nickname for variable inside of function
void double( int *number )
{
*number = 2 * ( *number );
}
– *number used as nickname for the variable passed
Program Output
2000 Prentice Hall, Inc.
All rights reserved.
7.5 Using the const Qualifier with
Pointers
• const qualifier
– Variable cannot be changed
– Use const if function does not need to change a variable
– Attempting to change a const variable produces an error
• const pointers
– Point to a constant memory location
– Must be initialized when declared
– int *const myPtr = &x;
• Type int *const – constant pointer to an int
– const int *myPtr = &x;
• Regular pointer to a const int
– const int *const Ptr = &x;
• const pointer to a const int
• x can be changed, but not *Ptr
2000 Prentice Hall, Inc.
All rights reserved.
1 /* Fig. 7.13: fig07_13.c
2 Attempting to modify a constant pointer to Outline
3 non-constant data */
4 1. Declare
5 #include <stdio.h> variables
6
7 int main() 1.1 Declare const
8 {
Changing *ptr is allowed – x is pointer to an int
9 int x, y;
not a constant.
10
2. Change *ptr
11 int * const ptr = &x; /* ptr is a constant pointer to an
(which is x)
12 integer. An integer can be modified
13 through ptr, but ptr always points
2.1 Attempt to
14 to the same memory location. */
change ptr
15 *ptr = 7;
Changing ptr is an error –
16 ptr = &y;
ptr is a constant pointer. 3. Output
17
18 return 0;
19 }
FIG07_13.c:
Error E2024 FIG07_13.c 16: Cannot modify a const object in
function main
Program Output
*** 1 errors in Compile ***
3
This program puts values into an array, sorts the values into
12 int i;
2. Print array
Bubblesort gets passed the
13
address of array elements
14 printf( "Data items in original order\n" );
(pointers). The name of an 2.1 Call bubbleSort
15
21
23 printf( "%4d", a[ i ] );
24
25 printf( "\n" );
26
27 return 0;
28 }
29
suit[1] ’D’ ’i’ ’a’ ’m’ ’o’ ’n’ ’d’ ’s’ ’\0’
– suit array has a fixed size, but strings can be of any size
2000 Prentice Hall, Inc.
All rights reserved.
7.10 Case Study: A Card Shuffling and
Dealing Simulation
• Card shuffling program
– Use array of pointers to strings
– Use double scripted array (suit, face)
Ace Two Three Four Five Six Seven Eight Nine Ten Jack Queen King
0 1 2 3 4 5 6 7 8 9 10 11 12
Hearts 0
Diamonds 1
Clubs 2
Spades 3
Clubs King
3 #include <stdio.h>
Outline
4 #include <stdlib.h>
8 void deal( const int [][ 13 ], const char *[], const char *[] );
20 srand( time( 0 ) );
21
3. Define functions
22 shuffle( deck );
24
25 return 0;
26 }
27
29 {
3 #include <stdio.h>
Outline
4 #define SIZE 10
5 void bubble( int [], const int, int (*)( int, int ) ); 1. Initialize array
6 int ascending( int, int );
12 int order,
24 if ( order == 1 ) {
27 }
28 else {
34
35
printf( "%5d", a[ counter ] );
Outline
36 printf( "\n" );
37 3.1 Define
38 return 0; functions
39 }
40
41 void bubble( int work[], const int size, ascending and descending
42 int (*compare)( int, int ) ) return true or false.
43 { bubble calls swap if the
44 int pass, count;
function call returns true.
45
47
49
51
61 *element1Ptr = *element2Ptr;
62 *element2Ptr = temp;
66 { Outline
67 return b < a; /* swap if b is less than a */
3.1 Define
68 }
functions
69
71 {
73 }