[go: up one dir, main page]

0% found this document useful (0 votes)
72 views24 pages

Fundamentals of Computer Science: Lecture 21: Pointers

1. The document discusses pointers in C programming. It defines a pointer as a variable that contains the address of another variable. 2. It explains how to declare pointers using syntax like int *xPtr and covers the address & and indirection * operators. & returns the address of a variable and * returns the value at a pointer's address. 3. Call by reference is described as passing a pointer (address) to a function rather than passing a copy of the variable. This allows the function to modify the original variable. Examples of call by reference include a cube and swap function.

Uploaded by

piyiwi4620
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)
72 views24 pages

Fundamentals of Computer Science: Lecture 21: Pointers

1. The document discusses pointers in C programming. It defines a pointer as a variable that contains the address of another variable. 2. It explains how to declare pointers using syntax like int *xPtr and covers the address & and indirection * operators. & returns the address of a variable and * returns the value at a pointer's address. 3. Call by reference is described as passing a pointer (address) to a function rather than passing a copy of the variable. This allows the function to modify the original variable. Examples of call by reference include a cube and swap function.

Uploaded by

piyiwi4620
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/ 24

Fundamentals of Computer Science

Lecture 21: Pointers


Ethan Cerami
New York University
Road-Map
• Computer Architecture
• What is a Pointer?
• Declaring Pointers
• Pointer Operators
– Address Operator
– Indirection Operator
• Calling Functions by Reference
– Cube Function
– Swap Function

• Reading: Chapter 7: Sections 7.1- 7.4


Basic Computer Architecture
Computer Architecture

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

int x is placed at location


1005.
Introduction to Pointers
What’s a Pointer?
• Pointer: variable that contains the address of
another variable.
• For example, you could have two variables: x,
xPtr.

xPtr x

• Here, we say that xPtr “points to” x.


• Or xPtr contains the address of x.
Declaring Pointers
* Indicates that this is a pointer.
• To declare a pointer, use the following template:
data_type *var_name;
• Pointers can point to any data type: int, char, float,
double, long, etc.
• Examples:
– int *xPtr;
• “xPtr is a pointer to type int.”
– double *salesPtr;
• “salesPtr is a pointer to type double.”
Pointer Operators
• Now we know how to declare pointers, but to use
them you must understand the two pointer
operators:
– & Address Operator. Returns the address of a
variable.
– * Indirection Operator. Returns the value
referenced by a pointer.
Pointer Operators in Action
• int x = 5;
• int *xPtr = NULL; NULL: xPtr points
to Nothing

NULL 5

0 1 2 3 4

xPtr is located x is located at


at address: 0 address: 2
Using the Address & Operator
• Now, you can apply the Address & operator.
• &x Determines the address of x
• xPtr = &x; xPtr now points to x

2 5

0 1 2 3 4

xPtr x is located at address 2


Using the * Indirection Operator

• If you want to determine the value referenced by a


pointer, you apply the * or indirection operator.
• printf (“%d”, *xPtr); This prints “5”

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 }

The address of a is 0012FF88


The value of aPtr is 0012FF88 Program Output
The value of a is 7
The value of *aPtr is 7
Example (Cont.)
• aPtr “points” to a.

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 }

The original value of number is 5


The new value of number is 125 Program Output
Limitations of Call by Value
• You can only return a single value from a
function.
• So, what happens if you want to return two values,
three values, more?
• The only way around this is to use pointers.
Example

• Suppose you want to create a simple function that


simply swaps two numbers.
• For example, you give it the numbers: 4,5. And
the function returns 5,4.
• This is particularly useful for sorting programs.
• Only problem: there is no way to do this with Call
by Value, because you need to modify and return
two values.
Pointers and Call by Reference
• To get around this problem, we use pointers.

void swap(int *element1Ptr, int *element2Ptr)


{
int temp;
* Indicates that
temp = *element1Ptr; we are passing
*element1Ptr = *element2Ptr; pointers or
*element2Ptr = temp; addresses.
}
Swapping with Pointers
• To use swap in a program:
Instead of
main () { passing the actual
values, we pass
int x = 5; the addresses of
int y = 4; the variables.
printf ("Original Order: %d,%d\n", x,y);
swap (&x, &y);
printf ("After Swapping: %d,%d", x,y);
}
Swapping in Action
• Within main: swap (&x, &y);
• within swap:
– element1Ptr now points to x
– element2Ptr now points to y

– temp = *element1Ptr; sets temp =5;


Before 5
– *element1Ptr = *element2Ptr; sets x = 4;
4
Swap:– *element2Ptr = temp; sets y = 5;

element1Ptr x element2Ptr y
Swapping Program Output

Original Order: 4,5


After Swapping: 5,4

You might also like