Vous êtes sur la page 1sur 8

Department of Computer and Information Science,

School of Science, IUPUI

CSCI 230

Pointers
Call-by-Reference
Dale Roberts, Lecturer
Computer Science, IUPUI
E-mail: droberts@cs.iupui.edu

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

/*

Cube a variable using call-by-value

*/

4
5 #include <stdio.h>
6
7 int cubeByValue( int );

/* prototype */

Function prototype

8
9 int main()
10 {
11

Initialize variables

int number = 5;

12
13

printf( "The original value of number is %d", number );

14

number = cubeByValue( number );

15

printf( "\nThe new value of number is %d\n", number );

Call function

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
The new value of number is 125

Dale Roberts

Program Output

Example of Calling Functions by Value


int main()
{
int number = 5;
number = cubeByValue( number );
}

number

int main()
{
int number = 5;
number = cubeByValue( number );
}

number

int main()
{
int number = 5;
number = cubeByValue( number );
}
int main()
{
int number = 5;
number = cubeByValue( number );
}

int main()
{
int number = 5;
number = cubeByValue( number );
}

number
5

number
5

number
125

Dale Roberts

int cubeByValue( int n )


{
return n * n * n;
}
int cubeByValue( int n )
{
return n * n * n;
}
int cubeByValue( int n )
{
return n * n * n;
}
125
int cubeByValue( int n )
{
return n * n * n;
}
int cubeByValue( int n )
{
return n * n * n;
}

n
undefined

n
5

n
5

n
undefined

n
undefined

Example of Calling Functions by Reference


1 /* Fig. 7.7: fig07_07.c
2

Cube a variable using call-by-reference

with a pointer argument */

4
5 #include <stdio.h>

Function prototype

6
7 void cubeByReference( int * );
8

Notice that the function prototype takes


a pointer to an integer ( int * ).

9 int main()
10 {
11

/* prototype */

int number = 5;

Initialize variables

12
13

printf( "The original value of number is %d", number );

14

cubeByReference( &number );

15

printf( "\nThe new value of number is %d\n", number );

Call function

16
17
18 }

return 0;

Inside cubeByReference, *nPtr is used (*nPtr is number).

19
20 void cubeByReference( int *nPtr )

Define function

21 {
22

Notice how the address of


number is given cubeByReference expects
a pointer (an address of a
variable).

*nPtr = *nPtr * *nPtr * *nPtr;

23 }
The original value of number is 5
The new value of number is 125

/* cube number in main */

Program Output

Dale Roberts

Example of Calling Functions by Reference


Before the call by reference to cubeByReference:

int main()
{
int number = 5;
cubeByReference( &number );
}

number
5

void cubeByReference( int *nPtr )


{
*nPtr = *nPtr * *nPtr * *nPtr;
}

nPtr
undefined

After call by reference to cubeByReference and before *nPtr is cubed:

int main()
{
int number = 5;
cubeByReference( &number );
}

number
5

n
void cubeByReference( int *nPtr )
{
*nPtr = *nPtr * *nPtr * *nPtr;
}

nPtr
address of
number

After *nPtr is cubed :

int main()
{
int number = 5;
cubeByReference( &number );
}

number
125

void cubeByReference( int *nPtr )


{
*nPtr = *nPtr * *nPtr * *nPtr;
}

Dale Roberts

nPtr
address of
number

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
1)

COMPUTER MEMORY

Point to a constant memory location


Must be initialized when declared
int *const myPtr1 = &x1;
Type int *const
Constant pointer to an int
x can be changed, but not *Ptr

2)

Ptr

const int *const Ptr = &x3;


const pointer to a const int

Dale Roberts

x3

case 3

myPtr1

x2
case
2

case
1

const int *myPtr2 = &x2;


Regular pointer to a const int

3)

CONSTANT MEMORY AREA

x1
case with
using const

myPtr

VARIABLE MEMORY AREA

myPtr2

/* Fig. 7.13: fig07_13.c

Attempting to modify a constant pointer to

non-constant data */

4
5

#include <stdio.h>

6
7

int main()

Declare variables

int x, y;

10
11

int * const ptr = &x; /* ptr is a constant pointer to an

12

integer. An integer can be modified

13

through ptr, but ptr always points

14

to the same memory location. */

15

*ptr = 7;

16

ptr = &y;

Changing *ptr is allowed x is


not a constant.

17
18
19 }

return 0;

Changing ptr is an error ptr is


a constant pointer.

Declare const
pointer to an int

Change *ptr (which


is x)
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 ***

Dale Roberts

Output

Vous aimerez peut-être aussi