Professional Documents
Culture Documents
Fundamentals of Computer Science: Lecture 21: Pointers
Fundamentals of Computer Science: Lecture 21: Pointers
Fundamentals of Computer Science: Lecture 21: Pointers
CPU: RAM:
“Brains” of
Short-term
the Computer
memory
When you run your computer, RAM (Random Access
Memory) stores data for your operating system and all
the applications you are running.
RAM (Random Access Memory)
• Every spot in memory has a unique address.
• Declaring Variables: When you declare a
variable, the operating system allocates a specific
spot in memory for that variable.
• For example, suppose you have int x = 5;
... 5 ...
1000
xPtr x
NULL 5
0 1 2 3 4
2 5
0 1 2 3 4
2 5
0 1 2 3 4
xPtr x
1 /* Simplified Version of Fig. 7.4: fig07_04.c
2 Using the & and * operators */
3 #include <stdio.h>
4
5 int main() 1. Declare variables
6 { Determining the address of a.
7 int a; /* a is an integer */
8 int *aPtr; /* aPtr is a pointer to an integer */
2 Initialize variables
9
10 a = 7; Determining the value of a.
3. Print
11 aPtr = &a; /* aPtr set to address of a */
12
13 printf( "The address of a is %p"
14 "\nThe value of aPtr is %p", &a, aPtr );
15
16 printf( "\n\nThe value of a is %d"
17 "\nThe value of *aPtr is %d", a, *aPtr );
18
22
23 return 0;
24 }
7
aPtr a
Program Output:
The address of a is 4F67:2240
The value of aPtr is 4F67:2240
These are
Hexadecimal
The value of a is 7 Values. %p is
the format code
The value of *aPtr is 7 for Pointer
Addresses
Return to Call by Reference
Call by Reference
• Call by Value:
– When you pass a variable, you pass a copy of the
variable.
– Changes to the copy do not affect the original value.
• Call by Reference:
– When you pass a variable, you pass a reference
(pointer) to the original variable.
– Changes to the reference (pointer) do affect the
original variable.
Calling Functions by Reference
• Call by reference with pointer arguments
– Pass address of argument using & operator
– Allows you to change actual location in memory
• * 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 Notice that the
Cube a variable using call-by-reference function prototype
3 with a pointer argument */ takes a pointer to an integer (int *).
4
1. Function prototype
5 #include <stdio.h>
6 Notice how the address of
7 void cubeByReference( int * ); /* number */
prototype is given - 1.1 Initialize variables
8 cubeByReference expects a
9 int main() pointer (an address of a variable). 2. Call function
10 {
11 int number = 5;
12 3. Define function
13 printf( "The original value of number is %d", number );
14 cubeByReference( &number );
15 printf( "\nThe new value of number is %d\n", number );
16 Inside cubeByReference, *nPtr
17 return 0; is used (*nPtr is number).
18 }
19
20 void cubeByReference( int *nPtr )
21 {
22 *nPtr = *nPtr * *nPtr * *nPtr; /* cube number in main */
23 }
element1Ptr x element2Ptr y
Swapping Program Output