0% found this document useful (0 votes)
3 views

CHTP5e_07-Pointers 1

The document provides an overview of C pointers, including their definitions, initialization, and usage in function arguments. It discusses pointer operators, common programming errors, and good practices for using pointers effectively. Additionally, it includes examples demonstrating call-by-reference and the relationship between pointers and arrays.

Uploaded by

23145002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

CHTP5e_07-Pointers 1

The document provides an overview of C pointers, including their definitions, initialization, and usage in function arguments. It discusses pointer operators, common programming errors, and good practices for using pointers effectively. Additionally, it includes examples demonstrating call-by-reference and the relationship between pointers and arrays.

Uploaded by

23145002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21

1

7
C Pointers
2

7.1 Introduction
7.2 Pointer Variable Definitions and Initialization
7.3 Pointer Operators
7.4 Passing Arguments to Functions by Reference
7.5 Using the const Qualifier with Pointers
7.6 Bubble Sort Using Call-by-Reference
7.7 sizeof Operator
7.8 Pointer Expressions and Pointer Arithmetic
7.9 Relationship between Pointers and Arrays
7.10 Arrays of Pointers
7.11 Case Study: Card Shuffling and Dealing Simulation
7.12 Pointers to Functions
3

7.1 Introduction
 Pointers
• Powerful, but difficult to master
• Simulate call-by-reference
• Close relationship with arrays and strings
4

7.2 Pointer Variable Definitions and


Initialization
 Pointer variables
• Contain memory addresses as their values
• Normal variables contain a specific value
(direct reference)
• Pointers contain address of a variable that has
a specific value (indirect reference)
• Indirection – referencing a pointer value
5

Directly and indirectly referencing a variable.


6

7.2 Pointer Variable Definitions, 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 variabl
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)
7

Common Programming Error


• The asterisk (*) notation used to declare pointer
variables does not distribute to all variable names
in a declaration

• Each pointer must be declared with the *


prefixed to the name; e.g., if you wish to declare
xPtr and yPtr as int pointers, use int *xPtr,
*yPtr;
8

Good Programming Practice


• Include the letters ptr in pointer variable names
to make it clear that these variables are pointers
and thus need to be handled appropriately

Error-Prevention Tip

• Initialize pointers to prevent unexpected


results
9

7.3 Pointer Operators


 & (address operator)
• Returns address of operand
int y = 5;
int *yPtr;
yPtr = &y;
/* yPtr gets address of y */
yPtr “points to” y
10

Graphical representation of a pointer pointing to an integer variable in memory.

Representation of y and yPtr in memory.


11

7.3 Pointer Operators


 * (indirection/dereferencing operator)
• Returns a synonym/alias of what its operand
points to
• *yptr returns y (because yptr points to y)
• * can be used for assignment
Returns alias to an object
*yptr = 7; /* changes y to 7 */
 * and & are inverses
• They cancel each other out
12

Common Programming Error


• Dereferencing a pointer that has not been
properly initialized or that has not been assigned
to point to a specific location in memory is an
error

• This could cause a fatal execution-time error, or it


could accidentally modify important data and
allow the program to run to completion with
incorrect results
1 /* Fig. 7.4: fig07_04.c 13
2 Using the & and * operators */
3 #include <stdio.h>
4
5 int main( void )
6 {
7 int a; /* a is an integer */
8 int *aPtr; /* aPtr is a pointer to an integer */
9
10 a = 7;
11 aPtr = &a; /* aPtr set to address of a */
12
13 printf( "The address of a is %p"
If aPtr points to a, then &a and
14 "\nThe value of aPtr is %p", &a, aPtr );
aPtr have the same value.
15
16 printf( "\n\nThe value of a is %d"
a and *aPtr have the
17 "\nThe value of *aPtr is %d", a, *aPtr );
same value
18
19 printf( "\n\nShowing that * and & are complements of "
20 "each other\n&*aPtr = %p"
21 "\n*&aPtr = %p\n", &*aPtr, *&aPtr ); &*aPtr and *&aPtr have the
22 same value
23 return 0; /* indicates successful termination */
24
25 } /* end main */
14
The address of a is 0012FF7C
The value of aPtr is 0012FF7C

The value of a is 7
The value of *aPtr is 7

Showing that * and & are complements of each other.


&*aPtr = 0012FF7C
*&aPtr = 0012FF7C
15

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
1 /* Fig. 7.6: fig07_06.c 16
2 Cube a variable using call-by-value */
3 #include <stdio.h>
4
5 int cubeByValue( int n ); /* prototype */
6
7 int main( void )
8 {
9 int number = 5; /* initialize number */
10
11 printf( "The original value of number is %d", number );
12
13 /* pass number by value to cubeByValue */
14 number = cubeByValue( number );
15
16 printf( "\nThe new value of number is %d\n", number );
17
18 return 0; /* indicates successful termination */
19
20 } /* end main */
21
22 /* calculate and return cube of integer argument */
23 int cubeByValue( int n )
24 {
25 return n * n * n; /* cube local variable n and return result */
26
27 } /* end function cubeByValue */

The original value of number is 5


The new value of number is 125
17

Common Programming Error


• Not dereferencing a pointer when it is necessary
to do so in order to obtain the value to which the
pointer points is a syntax error
1 /* Fig. 7.7: fig07_07.c 18
2 Cube a variable using call-by-reference with a pointer argument */
3
4 #include <stdio.h> Function prototype takes a pointer argument
5
6 void cubeByReference( int *nPtr ); /* prototype */
7
8 int main( void )
9 {
10 int number = 5; /* initialize number */
11
12 printf( "The original value of number is %d", number );
13
Function cubeByReference is
14 /* pass address of number to cubeByReference */
15 cubeByReference( &number );
passed an address, which can be the
16 value of a pointer variable
17 printf( "\nThe new value of number is %d\n", number );
18
19 return 0; /* indicates successful termination */
20
21 } /* end main */
22
23 /* calculate cube of *nPtr; modifies variable number in main */
24 void cubeByReference( int *nPtr )
In this program, *nPtr is number, so this
25 {
26 *nPtr = *nPtr * *nPtr * *nPtr; /* cube *nPtr */
statement modifies the value of number
27 } /* end function cubeByReference */ itself.

The original value of number is 5


The new value of number is 125
19

Analysis of a typical call-by-value.


20

Analysis of a typical call-by-reference with a pointer argument.


21

Error-Prevention Tip

• Use call-by-value to pass arguments to a function


unless the caller explicitly requires the called
function to modify the value of the argument
variable in the caller’s environment

• This prevents accidental modification of the


caller’s arguments and is another example of the
principle of least privilege

You might also like