0% found this document useful (0 votes)
56 views44 pages

Pointers and Reference Parameters: CSE 251 Dr. Charles B. Owen Programming in C 1

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 44

Pointers 

and Reference parameters

CSE 251 Dr. Charles B. Owen
1 Programming in C
Today’s Lecture

Address Variable Value


Pointers are variables that 
0 4520
0x4520 B 181
store memory addresses
0x4524 pB 0x4520

Memory Address

Before we learn about  Address Variable Value


pointers, we must learn  0x4520 B 181
more about addresses 0x4524 pB 0x4520

Pointer Value

CSE 251 Dr. Charles B. Owen
2 Programming in C
Computer Memory

Memory is just a long list of 
numbers one after the other
My laptop has over 4 Billion of 
these numbers
these numbers

Each number is 8 bits (BYTE)
We combine them to make 
integers and floating point values
integers and floating point values

CSE 251 Dr. Charles B. Owen
3 Programming in C
Computer 
Memory

int (21) float (18.2)

CSE 251 Dr. Charles B. Owen
4 Programming in C
Memory Addresses
Memory addresses in computers are often 32 bits (or 
nowadays, 64‐bits) long, e.g.  

01111111111111111111101010001100

Another way to represent an address is to use 
hexadecimal: 

0x 7ffffa8c

CSE 251 Dr. Charles B. Owen
5 Programming in C
0000 = 0  1000 = 8
Hexadecimal (Base‐16)
0001 = 1 1001 = 9
0010 = 2 1010 = 10 = a
I have included this 
chart on your 
y 0011 = 3 1011 = 11 = b
worksheet so you can  0100 = 4 1100 = 12 = c
refer to it. 0101 = 5 1101 = 13 = d
0110 6
0110 = 6 1110 14
1110 = 14 = e
0111 = 7 1111 = 15 = f

CSE 251 Dr. Charles B. Owen
6 Programming in C
Addresses
32‐bit address (Binary): 
b dd ( )
0111 1111 1111 1111 1111 1010 1000 1100
7        f       f f f a        8        c

32‐bit address (Hex): 0x 7 f f f f a 8 c
Notes:
– In C “0x” indicates a Hexadecimal number
– Convert every four bits to a hex digit

CSE 251 Dr. Charles B. Owen
7 Programming in C
Arithmetic (in Hex) 0000 = 0  1000 = 8
0001 = 1
0001  1 1001 = 9
1001  9
Sum:  0x 90  0010 = 2 1010 = 10 = a
+ 0x 04  0011 = 3 1011 = 11 = b
0x 94 0100 = 4 1100 = 12 = c
0101 = 5 1101 = 13 = d
0110 = 6 1110 = 14 = e
0111 = 7 1111 = 15 = f

CSE 251 Dr. Charles B. Owen
8 Programming in C
Arithmetic (in Hex) 0000 = 0  1000 = 8
0001 = 1
0001  1 1001 = 9
1001  9
Sum with carry:   0x 8c  0010 = 2 1010 = 10 = a
+ 0x 04  0011 = 3 1011 = 11 = b
0x ?? 0100 = 4 1100 = 12 = c
0101 = 5 1101 = 13 = d
0110 = 6 1110 = 14 = e
0111 = 7 1111 = 15 = f

CSE 251 Dr. Charles B. Owen
9 Programming in C
Arithmetic (in Hex)
Sum with carry:   0x 8c 
+ 0x 04 
0x ??

– What is “c + 4”? 
– In decimal it is “12 + 4 = 16”  
which is Hex “10” (0 and carry 1)
hi h i H “10” (0 d 1)

CSE 251 Dr. Charles B. Owen
10 Programming in C
Arithmetic (in Hex)
1
Sum with carry:   0x 8c 
+ 0x 04 
0x ??

– What is “c + 4”? 
– In decimal it is “12 + 4 = 16”  
which is Hex “10” (0 and carry 1)
hi h i H “10” (0 d 1)

CSE 251 Dr. Charles B. Owen
11 Programming in C
Arithmetic (in Hex)
1
Sum with carry:   0x 8c 
+ 0x 04 
0x 90

– What is “c + 4”? 
– In decimal it is “12 + 4 = 16”  
which is Hex “10” (0 and carry 1)
hi h i H “10” (0 d 1)

CSE 251 Dr. Charles B. Owen
12 1 Programming in C
Bytes and Words
byte
word

Remember
8 bits = 1 byte
32 bits = 4 bytes = 1 word.
32‐bit address machines are 
addressed by bytes so 
consecutive words have
consecutive words have 
addresses that differ by four

CSE 251 Dr. Charles B. Owen
13 Programming in C
32‐bit Addresses
H
Here are three consecutive 32‐bit addresses (in Hex) of words:
h i 32 bi dd (i H ) f d

0x00712050 dc bb 21 00
0x00712054 01 00 00 00
0x00712058 00 00 00 00

CSE 251 Dr. Charles B. Owen
14 Programming in C
Pointers
Pointers are variables that contain addresses
Just like other variables, they must be declared before 
being used
being used

Declaration:  
int *p;      /* instead of int p for integers */

int * means p is a pointer variable that stores the address 
* means p is a pointer variable that stores the address
of an integer variable

Pointers and Ref 
CSE 251 Dr. Charles B. Owen
15 parameters
Programming in C
Pointer Initialization
Declaration:       
int a = 2; /* a is an integer */
i t *pA
int * A = &a;
& /* pA
/* A is a pointer containing  
i i t t i i
the address of a */

“&” operator means “address of”
Read it as “at”
Read it as “at”

CSE 251 Dr. Charles B. Owen
16 Programming in C
int a = 2; 
The Address Game int *pA = &a;
a

p
pA

An Intel processor is called a little endian
processor because it stores values with the least
processor because it stores values with the least 
significant byte first. You read it in reverse order. 

0x00602104 in memory will be: 04 21 60 00

CSE 251 Dr. Charles B. Owen
17 Programming in C
“%x” prints the hexadecimal value
Example Program
Operators:
p
& “address of”
int a = 21;
int *pA = &a; * “dereference”

Output
printf("%d\n", a); 21
printf("%x\n", a); 15
printf("% \n" &a);
printf("%x\n", &a); bfee861c
f
printf("%x\n", pA); bfee861c
printf("%d\n", *pA); 21
printf("%x\n", &pA);
p ( p ) bfee8618

CSE 251 Dr. Charles B. Owen
18 Programming in C
#include<stdio.h>
int main()  
{
int a = 15, b = 38;
int *c = &a;
p
printf("%x : %d\n", &a, a);
( \ , , );
printf("%x : %d\n", &b ,b);
printf("%x : %x : %d\n", &c, c, *c);
aa = 49;
= 49;
printf("%x : %d\n", &a, a);
printf("%x : %d\n", &b ,b);
p
printf("%x : %x : %d\n", &c, c, *c);
( \ , , , );
c = &b;
printf("%x : %d\n", &a, a);
printf("%x
printf( %x : %d\n
: %d\n", &b ,b);
&b b);
printf("%x : %x : %d\n", &c, c, *c);
}

CSE 251 Dr. Charles B. Owen
19 Programming in C
#include<stdio.h>
int main()  
{
int a = 15, b = 38;
int *c = &a;
p
printf("%x : %d\n", &a, a);
( \ , , );
printf("%x : %d\n", &b ,b);
printf("%x : %x : %d\n", &c, c, *c);
aa = 49;
= 49;
printf("%x : %d\n", &a, a);
printf("%x : %d\n", &b ,b);
p
printf("%x : %x : %d\n", &c, c, *c);
( \ , , , );
c = &b;
printf("%x : %d\n", &a, a);
printf("%x
printf( %x : %d\n
: %d\n", &b ,b);
&b b);
printf("%x : %x : %d\n", &c, c, *c);
}

CSE 251 Dr. Charles B. Owen
20 Programming in C
Declares a and b as integers
First Section Declares c as a pointer that contains the 
address of a (
of a (“points
points to a
to a”)) 

Address Memory Name


int a = 15, b = 38;
a = 15 b = 38;
int *c = &a; 0xeffffa94 15 a
printf("%x : %d\n", &a, a);
0 ffff 90
0xeffffa90 38 b
printf("%x : %d\n", &b ,b);
printf("%x : %x : %d\n", &c, c, *c); 0xeffffa94
0xeffffa8c c

CSE 251 Dr. Charles B. Owen
21 Programming in C
Declares a and b as integers
First Section Declares c as a pointer that contains the 
address of a (
of a (“points
points to a
to a”)) 

Address Memory Name


int a = 15, b = 38;
a = 15 b = 38;
int *c = &a; 0xeffffa94 15 a
printf("%x : %d\n", &a, a);
0 ffff 90
0xeffffa90 38 b
printf("%x : %d\n", &b ,b);
printf("%x : %x : %d\n", &c, c, *c); 0xeffffa94
0xeffffa8c c

Output:
effffa94  15
effffa90  38
effffa8c  effffa94  15

CSE 251 Dr. Charles B. Owen
22 Programming in C
Declares a and b as integers
First Section Declares c as a pointer that contains the 
address of a (
of a (“points
points to a
to a”)) 

int a = 15, b = 38;
a = 15 b = 38; Note the difference between 
Note the difference between
int *c = &a; *C in variable declaration 
printf("%x : %d\n", &a, a); and *C in printf
printf("%x : %d\n", &b ,b);
printf("%x : %x : %d\n", &c, c, *c);

Output:
effffa94  15
effffa90  38
effffa8c  effffa94  15

CSE 251 Dr. Charles B. Owen
23 Programming in C
#include<stdio.h>
int main()  
{
int a = 15, b = 38;
int *c = &a;
p
printf("%x : %d\n", &a, a);
( \ , , );
printf("%x : %d\n", &b ,b);
printf("%x : %x : %d\n", &c, c, *c);
aa = 49;
= 49;
printf("%x : %d\n", &a, a);
printf("%x : %d\n", &b ,b);
p
printf("%x : %x : %d\n", &c, c, *c);
( \ , , , );
c = &b;
printf("%x : %d\n", &a, a);
printf("%x
printf( %x : %d\n
: %d\n", &b ,b);
&b b);
printf("%x : %x : %d\n", &c, c, *c);
}

CSE 251 Dr. Charles B. Owen
24 Programming in C
Second Section
Address Memoryy Name
a = 49;
0xeffffa94 15 49 A
printf("%x : %d\n", &a, a);
printf("%x : %d\n", &b ,b);
38 B
printf("%x : %x : %d\n", &c, c, *c); 0xeffffa90

0xeffffa8c 0xeffffa94 C

CSE 251 Dr. Charles B. Owen
25 Programming in C
Second Example
Address Memoryy Name
a = 49;
0xeffffa94 15 49 A
printf("%x : %d\n", &a, a);
printf("%x : %d\n", &b ,b);
38 B
printf("%x : %x : %d\n", &c, c, *c); 0xeffffa90

0xeffffa8c 0xeffffa94 C

Output:
effffa94 49
effffa90 38
effffa8c effffa94 49

CSE 251 Dr. Charles B. Owen
26 Programming in C
#include<stdio.h>
int main()  
{
int a = 15, b = 38;
int *c = &a;
p
printf("%x : %d\n", &a, a);
( \ , , );
printf("%x : %d\n", &b ,b);
printf("%x : %x : %d\n", &c, c, *c);
aa = 49;
= 49;
printf("%x : %d\n", &a, a);
printf("%x : %d\n", &b ,b);
p
printf("%x : %x : %d\n", &c, c, *c);
( \ , , , );
c = &b;
printf("%x : %d\n", &a, a);
printf("%x
printf( %x : %d\n
: %d\n", &b ,b);
&b b);
printf("%x : %x : %d\n", &c, c, *c);
}

CSE 251 Dr. Charles B. Owen
27 Programming in C
Third Section Address Memory Name

c = &b;  /* c now points to b */ 0xeffffa94 49 A
printf("%x : %d\n", &a, a);
printf("%x : %d\n", &b ,b); 0xeffffa90 38 B
printf("%x : %x : %d\n", &c, c, *c);
i f("% % %d\ " & * )
0xeffffa8c 0xeffffa90 C

CSE 251 Dr. Charles B. Owen
28 Programming in C
Third Section Address Memory Name

c = &b;  /* c now points to b */ 0xeffffa94 49 A
printf("%x : %d\n", &a, a);
printf("%x : %d\n", &b ,b); 0xeffffa90 38 B
printf("%x : %x : %d\n", &c, c, *c);
i f("% % %d\ " & * )
0xeffffa8c 0xeffffa90 C

Output:
O t t
effffa94 49
effffa90 38
effffa8c effffa90 38

CSE 251 Dr. Charles B. Owen
29 2 Programming in C
Reference parameters

A valuable use for pointers:
Passing addresses to a function

CSE 251 Dr. Charles B. Owen
30 Programming in C
Argument & Returned Value
Consider a function call y=f(x).                
– The value x is passed to the function f 
– A value is returned and assigned to y.

– By passedd we mean that the value of argument x is 
h h l f
copied to the parameter in the function.  Some 
calculation is performed and the result is returned and 
p
assigned to y.

CSE 251 Dr. Charles B. Owen
31 Programming in C
Example
int x, y;
x = 5; Address Memory Name
y = Square(x);
y = Square(x);
0xeffffa94 … x
int Square(int t) 
0xeffffa98 … y
{

return t*t …
}

CSE 251 Dr. Charles B. Owen
32 Programming in C
Example
int x, y;
x = 5; Address Memory Name
y = Square(x);
y = Square(x);
0xeffffa94 5 x
int Square(int t) 
0xeffffa98 … y
{

return t*t …
}

CSE 251 Dr. Charles B. Owen
33 Programming in C
The call Square(x) :
Example creates a variable t
copies the value of x to t
the value of x to t
int x, y;
x = 5; Address Memory Name
y = Square(x);
y = Square(x);
0xeffffa94 5 x
int Square(int t) 
0xeffffa98 … y

return t*t …
}

0xeffffc8c 5 t

CSE 251 Dr. Charles B. Owen
34 Programming in C
The call Square(x) :
Example creates a variable t
copies
p the value of x to t
int x, y; calculates t * t
x = 5; returns t
Address Memoryy Name
y = Square(x);
y = Square(x);
0xeffffa94 5 x
int Square(int t) 
0xeffffa98 … y

return t*t …
}

0xeffffc8c 5 t

0xeffffc90 25 temp

CSE 251 Dr. Charles B. Owen
35 Programming in C
y=f(x)
Only one valued returned 
What if we want to return more than one value?
– Solution is to use pointers to variables in the calling 
function

CSE 251 Dr. Charles B. Owen
36 Programming in C
How to do this in C
The approach is to pass the address (using the & 
operator) of the value to be modified.

We call such a parameter a reference parameter.

Use the * operator to change the reference parameter 
value

CSE 251 Dr. Charles B. Owen
37 Programming in C
Function Reference Params
Name Address Value
int val = 10;
val 0xeffffa90 10
MyFun(&val);
printf(“%d”,val);

void MyFun(int *param)
{
*param = 27;
}

CSE 251 Dr. Charles B. Owen
38 Programming in C
Function Reference Params
Name Address Value
int val = 10;
val 0xeffffa90 10
MyFun(&val);
printf(“%d”,val);

Name Address Value


void MyFun(int *param)
{
param 0xefffea88 0xefffa90
*param = 27;
}

CSE 251 Dr. Charles B. Owen
39 Programming in C
Function Reference Params
Name Address Value
int val = 10;
val 0xeffffa90 27
MyFun(&val);
printf(“%d”,val);

Name Address Value


void MyFun(int *param)
{
param 0xefffea88 0xefffa90
*param = 27;
}

CSE 251 Dr. Charles B. Owen
40 Programming in C
Function Reference Params
Name Address Value
int val = 10;
val 0xeffffa90 27
MyFun(&val);
printf(“%d”,val);

Prints: 27 void MyFun(int *param)


The memory used by  {
the function is  *param = 27;
destroyed when it  }
returns. 
returns

CSE 251 Dr. Charles B. Owen
41 Programming in C
What will this do different?
Name Address Value
int val = 10;
val 0xeffffa90 10
MyFun2(val);
printf(“%d”,val);

void MyFun2(int param)
{
param = 27;
}

CSE 251 Dr. Charles B. Owen
42 Programming in C
Cards program
/* C t d d d it */
/* Create a random card and suit */
/* This will compute a random number from 0 to 3 */
This program repeats code. We don’t 
suit1 = rand() % 4;
like to do that. But, we could not put 
the card draw into a function because 
h dd i
/* This will compute a random number from 1 to 13 */ f i b
card1 = rand() % 13 + 1; a function can only return one value, 
or so we thought!
do
{
/* Create a random card and suit */
/* This will compute a random number from 0 to 3 */
suit2 = rand() % 4;

/* This will compute a random number from 1 to 13 */
card2 = rand() % 13 + 1;
card2 = rand() % 13 + 1;

} while(card1 == card2 && suit1 == suit2);

CSE 251 Dr. Charles B. Owen
43 Programming in C
Solution, pass by reference using pointers
/* Create a random card and suit */
DrawCard(&card1, &suit1);

do
{
DrawCard(&card2, &suit2);
} while(card1 == card2 && suit1 == suit2);

void DrawCard(int *card, int *suit)


Don’t forget: {
/* This will compute a 
*suit ← to set the value
suit ← to set the value random number from 0 to 3 */
random number from 0 to 3  /
&card1 ← to get the address *suit = rand() % 4;

/* This will compute a random 
Pass with & number from 1 to 13 */
number from 1 to 13  /
Set with * *card = rand() % 13 + 1;
}

CSE 251 Dr. Charles B. Owen
44 3 Programming in C

You might also like