Lecture 07 C Pointers
Lecture 07 C Pointers
PROGRAMMING
FUNDAMENTALS (EE 170)
By Engr. Salman
Lecturer (Electrical Engineering)
NFC-IEFR, Faisalabad
Pointers
Outline
Introduction
Pointer Variable Definitions and Initialization
Pointer Operators
Calling Functions by Reference
Bubble Sort Using Call by Reference
Pointer Expressions and Pointer Arithmetic
The Relationship between Pointers and Arrays
Arrays of Pointers
Pointers to Functions
Objectives
countPtr count
7
Pointer Variable Definitions and
Initialization
Pointer definitions
* used with pointer variables
int *myPtr;
Defines a pointer to an int (pointer of type int *)
Multiple pointers require using a * before each
variable definition
int *myPtr1, *myPtr2;
Can define pointers to any data type
Initialize pointers to 0, NULL, or an address
0 or NULL – points to nothing (NULL preferred)
Pointer Operators
& (address operator)
Returns address of operand
int y = 5;
int *yPtr;
yPtr = &y; /* yPtr gets address of y */
yPtr “points to” y
y yptr y
5 500000 600000 600000 5
yPtr
Address of y
is value of
yptr
Pointer Operators
* (indirection/dereferencing operator)
Returns a synonym/alias of what its operand points to
*yptr returns y (because yptr points to y)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
The address of a is 0012FF7C
The value of aPtr is 0012FF7C
The value of a is 7
The value of *aPtr is 7
Program Output
Showing that * and & are complements of each other.
&*aPtr = 0012FF7C
*&aPtr = 0012FF7C
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
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
1 /* Fig. 7.7: fig07_07.c
2 Cube a variable using call-by-reference with a pointer argument */
3
Notice that the function prototype
4 #include <stdio.h>
takes a pointer to an integer. fig07_07.c
5
6 void cubeByReference( int *nPtr ); /* prototype */
7
8 int main()
9 {
10 int number = 5; /* initialize number */
11
12 printf( "The original value of number is %d", number );
13
14 /* pass address of number to cubeByReference */
15 cubeByReference( &number );
Notice how the address of
16
number is given -
17 printf( "\nThe new value of number is %d\n", number );
cubeByReference expects a
18
19 return 0; /* indicates successful termination */
pointer (an address of a variable).
20
21 } /* end main */
22
23 /* calculate cube of *nPtr; modifies variable number in main */ Inside cubeByReference, *nPtr is
24 void cubeByReference( int *nPtr ) used (*nPtr is number).
25 {
26 *nPtr = *nPtr * *nPtr * *nPtr; /* cube *nPtr */
27 } /* end function cubeByReference */
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
The original value of number is 5
The new value of number is 125
Program Output
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Before main calls cubeByReference :
int main() number void cubeByReference( int *nPtr )
{ {
5
int number = 5; *nPtr = *nPtr * *nPtr * *nPtr;
}
cubeByReference( &number ); nPtr
} undefined
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
27 /* loop through array a */
28 for ( i = 0; i < SIZE; i++ ) {
29 printf( "%4d", a[ i ] );
30 } /* end for */
31
fig07_15.c (Part 2 of
32 printf( "\n" );
3)
33
34 return 0; /* indicates successful termination */
35
36 } /* end main */
37
38 /* sort an array of integers using bubble sort algorithm */
39 void bubbleSort( int *array, const int size )
40 {
41 void swap( int *element1Ptr, int *element2Ptr ); /* prototype */
42 int pass; /* pass counter */
43 int j; /* comparison counter */
44
45 /* loop to control passes */
46 for ( pass = 0; pass < size - 1; pass++ ) {
47
48 /* loop to control comparisons during each pass */
49 for ( j = 0; j < size - 1; j++ ) {
50
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
51 /* swap adjacent elements if they are out of order */
52 if ( array[ j ] > array[ j + 1 ] ) {
53 swap( &array[ j ], &array[ j + 1 ] );
54 } /* end if */
55
fig07_15.c (Part 3 of
56 } /* end inner for */
3)
57
58 } /* end outer for */
59
60 } /* end function bubbleSort */
61
62 /* swap values at memory locations to which element1Ptr and
63 element2Ptr point */
64 void swap( int *element1Ptr, int *element2Ptr )
65 {
66 int hold = *element1Ptr;
67 *element1Ptr = *element2Ptr;
68 *element2Ptr = hold;
69 } /* end function swap */
Data items in original order
2 6 4 8 10 12 89 68 45 37
Data items in ascending order
2 4 6 8 10 12 37 45 68 89 Program Output
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Pointer Expressions and Pointer
Arithmetic
Arithmetic operations can be performed on pointers
Increment/decrement pointer (++ or --)
Add an integer to a pointer( + or += , - or -=)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
25 /* loop through array b */
26 for ( offset = 0; offset < 4; offset++ ) {
27 printf( "*( b + %d ) = %d\n", offset, *( b + offset ) );
28 } /* end for */
fig07_20.c (Part 2 of
29
2)
30 /* output array b using bPtr and array subscript notation */
31 printf( "\nPointer subscript notation\n" );
32
33 /* loop through array b */
34 for ( i = 0; i < 4; i++ ) {
35 printf( "bPtr[ %d ] = %d\n", i, bPtr[ i ] );
36 } /* end for */
37
38 /* output array b using bPtr and pointer/offset notation */
39 printf( "\nPointer/offset notation\n" );
40
41 /* loop through array b */
42 for ( offset = 0; offset < 4; offset++ ) {
43 printf( "*( bPtr + %d ) = %d\n", offset, *( bPtr + offset ) );
44 } /* end for */
45
46 return 0; /* indicates successful termination */
47
48 } /* end main */
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Array b printed with:
Array subscript notation
b[ 0 ] = 10
b[ 1 ] = 20
b[ 2 ] = 30
b[ 3 ] = 40
Program Output
Pointer/offset notation
*( bPtr + 0 ) = 10
*( bPtr + 1 ) = 20
*( bPtr + 2 ) = 30
*( bPtr + 3 ) = 40
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Arrays of Pointers
Arrays can contain pointers
For example: an array of strings
char *suit[ 4 ] = { "Hearts", "Diamonds",
"Clubs", "Spades" };
Strings are pointers to the first character
char * – each element of suit is a pointer to a char
suit[1] ’D’ ’i’ ’a’ ’m’ ’o’ ’n’ ’d’ ’s’ ’\0’
Questions/Answers
>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>
Acknowledgement
32