t13BPointersPassByReference Pps
t13BPointersPassByReference Pps
CSCI 230
Pointers
Call-by-Reference
Dale Roberts, Lecturer
Computer Science, IUPUI
E-mail: [email protected]
Dale Roberts
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 formal parameter for variable inside of function
void double_num ( int *number )
{
*number = 2 * ( *number );
}
*number used as nickname for the actual variable passed
Dale Roberts
Example of Calling Functions by Value
1 /*
2 Cube a variable using call-by-value
3 */
4
5 #include <stdio.h>
6
7 int cubeByValue( int ); /* prototype */ Function prototype
8
9 int main()
10 {
11 int number = 5; Initialize variables
12
13 printf( "The original value of number is %d", number );
14 number = cubeByValue( number ); Call function
15 printf( "\nThe new value of number is %d\n", number );
16
17 return 0;
18 }
19
20 int cubeByValue( int n )
Define function
21 {
22 return n * n * n; /* cube number in main */
23 }
The original value of number is 5 Program Output
The new value of number is 125
Dale Roberts
Example of Calling Functions by Value
int main() number int cubeByValue( int n )
{ {
int number = 5; n
5 return n * n * n;
number = cubeByValue( number );
} } undefined
Dale Roberts
Example of Calling Functions by Reference
1 /* Fig. 7.7: fig07_07.c
2 Cube a variable using call-by-reference
3 with a pointer argument */
4
5 #include <stdio.h>
6
Function prototype
7 void cubeByReference( int * ); /* prototype */
8
9 int main() Notice that the function prototype takes
a pointer to an integer ( int * ).
10 {
11 int number = 5;
12 Initialize variables
13 printf( "The original value of number is %d", number );
Notice how the address of
number is given -
14 cubeByReference( &number ); Call function cubeByReference expects
15 printf( "\nThe new value of number is %d\n", number );
a pointer (an address of a
16 variable).
17 return 0;
18 } Inside cubeByReference, *nPtr is used (*nPtr is number).
19
20 void cubeByReference( int *nPtr )
Define function
21 {
22 *nPtr = *nPtr * *nPtr * *nPtr; /* cube number in main */
23 }
The original value of number is 5 Program Output
The new value of number is 125
Dale Roberts
Example of Calling Functions by Reference
Dale Roberts
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 COMPUTER MEMORY
Point to a constant memory location CONSTANT MEMORY AREA
Ptr x3
Must be initialized when declared case 3
1) int *const myPtr1 = &x1; myPtr1 x2
Type int *const
Constant pointer to an int case case
1 2
x can be changed, but not *Ptr
2) const int *myPtr2 = &x2; x1 myPtr2
Regular pointer to a const int myPtr x
case with
3) const int *const Ptr = &x3; using const
Dale Roberts
1 /* Fig. 7.13: fig07_13.c
2 Attempting to modify a constant pointer to
3 non-constant data */
4
5 #include <stdio.h>
6
7 int main()
8 {
9 int x, y; Declare variables
10
11 int * const ptr = &x; /* ptr is a constant pointer to an Declare const
12 integer. An integer can be modified pointer to an int
13 through ptr, but ptr always points
14 to the same memory location. */
15 *ptr = 7; Changing *ptr is allowed x is Change *ptr (which
16 ptr = &y; not a constant. is x)
17
18 return 0; Changing ptr is an error ptr is
19 } a constant pointer. Attempt to change
ptr
FIG07_13.c:
Error E2024 FIG07_13.c 16: Cannot modify a const object in
function main
*** 1 errors in Compile ***