Pointers (Part 1) : Computer Science and The Art of Programming, Harvard School of
Pointers (Part 1) : Computer Science and The Art of Programming, Harvard School of
Programming Languages 1
Lecture 4
Pointers [ Part 1 ]
Course & Lectures are based on their counterparts in the following courses:
o Harvard University's CS50; An introduction to the intellectual enterprises of
computer science and the art of programming, Harvard School of
Engineering and Applied Sciences.
o UNSW's CS1: Higher Computing, by Richard Buckland – The University of
New South Wales.
o MIT's 6.087 Practical Programming in C (2010), 6.096 Introduction to C++
(2011), and 6.S096 Introduction to C and C++ (2013), MIT (Massachusetts
1
Institute of Technology) OpenCourseWare.
Pointers
• The most powerful features of the C programming
language
2
Memory address
Memory
• When we declare a variable address
int x = 5;
• It defines a variable of type int 1000
.
that holds an integer value. .
1500
• It reserve memory locations .
5 x
with specific memory address. .
Example: it reserves the memory .
6000
addresses 1500 to 1503 for the .
variable x. .
.
3
Pointer variable definition
• A Pointer contains a memory address of a variable.
• The definition:
Define an int (holds an integer value):
int count; count is a variable of type int
count ptr
5 5
Memory
address
int x =5;
int *ptr1 = NULL; For address
1000
// pointer initialization .
For address .
ptr1= &x; 1500 5 x
.
printf ("address of x is %p\n", &x ); .
printf ("ptr1 value is %p", ptr1 ); .
NULL
6000 1500 ptr1
ptr1 .
Logical
representation .
.
ptr1 x 5
Address of x is 1500
5 ptr1 value is 1500
Indirection
(dereferencing )
int *aPtr; 7
count
a = 7; 7
9
Pointer to pointer
3000
pptr ptr var
10
Pointer to pointer
3000
pptr ptr var
11
Pointer operators precedence
Pointer operators
12
Pointer Expressions
• Pointers are valid operands in:
• Assignment expressions ( = )
• Arithmetic expressions, only with:
Addition (+), increment (++), +=
Subtraction(-), decrement (--), -=
• Comparison expressions:
• Relational and equality operators.
but the pointers should point to elements of the
same object.
13
Pointer Assignments
int x = 6; p1 x
p2
16
Pointer Arithmetic:
Example 2
Memory
address
int a[5], x; p1
a[0] 3000
int *p1, *p2;
p1 = &a[0]; // p1 = a a[1] 3004
p2
p2 = &a[2]; a[2] 3008
17
pointer comparison Memory
address
int a[5]; p1
a[0] 3000
int *p1, *p2 = NULL;
p1 = &a[0]; a[1] 3004
p2
a[2] 3008
if ( p2 == NULL )
a[3] 3012
p2 = &a[2];
a[4] 3016
while ( p1 < p2 )
p2- -;
Now p1 equal to p2
18
Relation between Pointers and
Arrays
Arrays and pointers are related in C and often used interchangeably
Addresses Values
a
a+
a 0 = &a[0] 3000 8
a[0] = *(a+ 0) = 8
a+ 1 = &a[1] 3004 37
a[1] = *(a+ 1) = 37
a+ 2 = &a[2] 3008
19
a[2] = *(a+ 2) = 19
12
a+ 3 = &a[3] 3012
a[3] = *(a+ 3) = 12
45
a+ 4 = &a[4] 3016
a[4] = *(a+ 4) = 45
offset
It doesn’t
Pointer/offset Array subscript modify the
notation notation array name
Offset = array index 19
b
Using Subscripting and Pointer 10
21
b
Using Subscripting and PointerbPtr 10
Section 7.10 26
Thanks! .. Questions?