Lecture 9: C to LC-3
@NCStateECE
Connect to slido.com
#ece209
ECE 209
Computer Systems Programming
Spring 2021
Lecture 13: Pointers
Announcements
Quiz 3: 3/22
Program 2: coming soon…
Photo by Andrea Piacquadio from Pexels
https://www.pexels.com/@olly?utm_content=attributionCopyText&utm_medium=referral&utm_source=pexels
https://www.pexels.com/photo/cheerful-young-woman-screaming-into-megaphone-3761509/?utm_content=attributionCopyText&utm_medium=referral&utm_source=pexels
Pointers
Photo by Andrea Piacquadio from Pexels
Don’t be
scared!
A pointer is just an address.
https://www.pexels.com/@olly?utm_content=attributionCopyText&utm_medium=referral&utm_source=pexels
https://www.pexels.com/photo/cheerful-young-woman-screaming-into-megaphone-3761509/?utm_content=attributionCopyText&utm_medium=referral&utm_source=pexels
Pointers
Every language uses pointers — a lot!
C and C++ expose details of pointers
to the programmer
more than some other languages.
https://www.pexels.com/
x
y
z
a
Every variable has a
memory address.
xFC08
xFC07
xFC05
xFC04
We can calculate the address (if we need it)
using R5 (local) or R4 (global).
R5 →
Address-of Operator (&)
x
y
z
a
xFC08
xFC07
xFC05
xFC04
R5 →
Expression LC-3 Code
x LDR R0,R5,#0
&x ADD R0,R5,#0
& only works on variables or other L-values. Not for general expressions.
Pointer Variable
“Pointer” is not a type in C — must know what it’s pointing to.
Declare using the * qualifier.
int * p; // p is a pointer to an int
double * q; // q is a pointer to a double
char * r; // r is a pointer to a char
int * p; // p is a pointer to an int
double * q; // q is a pointer to a double
char * r; // r is a pointer to a char
p
q
r
Caution: When declared pointer is uninitialized, we don’t know where it points!
int * p; // p is a pointer to an int
double * q; // q is a pointer to a double
char * r; // r is a pointer to a char
p
The value of p is an address. It’s not an integer.
The value can change — it will point to something different.
The variable’s name is p. It’s not *p.
Dereference Operator (*)
If p is a pointer (address),
then *p is the value in the memory location pointed to.
p
value of p
in this box
(address)
value of *p
in this box
(int)
* only works for pointer expressions.
Expression LC-3 Code
p LDR R0,R5,#0
*p
LDR R0,R5,#0
LDR R0,R0,#0
p
value of p
in this box
(address)
value of *p
in this box
(int)
For this example, assuming p is local with offset = 0.
Dereference with Assignment
If *p is on the lefthand side of assignment,
then store to the memory location where p points.
p
p = …
changes this box
*p = …
changes this box
p
p = …
changes this box
*p = …
changes this box
Expression LC-3 Code
p = 0 STR R0,R5,#0
*p = 0
LDR R1,R5,#0
STR R0,R1,#0
For this example, assume R0 = 0 and p is local with offset = 0.
Photo by Andrea Piacquadio from Pexels
Still scared?
A pointer is just an address.
https://www.pexels.com/@olly?utm_content=attributionCopyText&utm_medium=referral&utm_source=pexels
https://www.pexels.com/photo/cheerful-young-woman-screaming-into-megaphone-3761509/?utm_content=attributionCopyText&utm_medium=referral&utm_source=pexels
int x = 10;
int y = 20;
int *p = &x; 10
20
x
y
p
It helps to visualize pointers.
The actual address doesn’t usually matter —
think about what where it’s pointing.
int x = 10;
int y = 20;
int *p = &x; 10
20
x
y
p
*p = 50;
50
20
x
y
p
p = &y;
50
20
x
y
p
*p = 100;
50
100
x
y
p
Nobody would write code like this…
Usage of Pointers
Access to array/string elements
Pass variables/arrays by reference
Dynamic memory allocation
Pointer-based data structures (linked list)
Array
int x[4];
x[0]
x[1]
x[2]
x[3]
x
Expression x (with no subscript)
is a pointer to the beginning
of the array.
Type of x is int*.
double a[3];
a[0]
a[1]
a[2]
Expression a (with no subscript)
is a pointer to the beginning
of the array.
Type of a is double*.
a
int x[4];
x[0]
x[1]
x[2]
x[3]
x
x+1
x+2
x+3
Subscript
Expression
Pointer
Expression
x[0] *x
x[2] *(x+2)
x[i] *(x+i)
int x[4];
x[0]
x[1]
x[2]
x[3]
x
x+1
x+2
x+3
Subscript
Expression
Pointer
Expression
&x[0] x
&x[2] x+2
&x[i] x+i
Subscript
Expression
Pointer
Expression
a[0] *a
a[2] *(a+2)
a[i] *(a+i)
double a[3];
a[0]
a[1]
a[2]
a
a+1
a+2
Pointer Arithmetic
For pointers, +1 does not literally add 1 to the address.
It adds the size of the “item” being referenced.
Based on type of pointer.
Specific value added is platform-specific.
But C-level behavior is platform-independent.
Always moves to the next “item” in memory.
Pointer Arithmetic
What operations can be done with a pointer?
pointer + integer
pointer – integer
pointer == pointer (<, >, <=, >=, !=)
pointer++, ++pointer
pointer–, –pointer
pointer – pointer (how many “items” between them?)
A pointer is not an integer!
Subscript
Expression
Pointer
Expression
a[0] *a
a[2] *(a+2)
a[i] *(a+i)
double a[3];
a[0]
a[1]
a[2]
a
a+1
a+2
Subscript
Expression
Pointer
Expression
x[0] *x
x[2] *(x+2)
x[i] *(x+i)
Do this. Don’t do this.
Just because you can,
doesn’t mean you should.
Array Traversal
int * ap = a;
int sum = 0;
for (i = 0; i < n; i++) {
sum = *ap;
ap += 1;
}
int sum = 0;
for (i = 0; i < n; i++) {
sum = a[i];
}
int * ap = a;
int sum = 0;
for (i = 0; i < n; i++) {
sum = *ap++;
}
No benefit.
Index notation is much
more understandable.
String Traversal
int count = 0;
char *sp = str;
while (*sp++) count += 1;
int count = 0;
int i = 0;
while (str[i]) {
count += 1;
i += 1;
}
Much more common for traversing string data in an array.
End of loop generally relies on the data, not on a count.
a l p h a \0str
char str[11];
sp sp sp sp sp sp
int count = 0;
char *sp = str;
while (*sp++) count += 1;
sp
Lecture 13: Pointers
Slide Number 2
Pointers
Pointers
Slide Number 6
Address-of Operator (&)
Pointer Variable
Slide Number 10
Slide Number 11
Dereference Operator (*)
Slide Number 14
Dereference with Assignment
Slide Number 16
Slide Number 18
Slide Number 19
Slide Number 20
Usage of Pointers
Array
Slide Number 24
Slide Number 25
Slide Number 26
Slide Number 27
Pointer Arithmetic
Pointer Arithmetic
Slide Number 30
Slide Number 32
Array Traversal
String Traversal
Slide Number 35