0% found this document useful (0 votes)
48 views

Pointers (Part 1) : Computer Science and The Art of Programming, Harvard School of

This lecture introduces pointers in C programming. Pointers contain the memory address of a variable. Pointer variables can be initialized and assigned memory addresses using the address-of operator (&). The indirection operator (*) is used to dereference a pointer and access the value of the variable it points to. Pointer arithmetic allows pointers to traverse through arrays by incrementing/decrementing the memory address. Pointers enable passing arguments by reference and dynamic memory allocation.

Uploaded by

Mohamed Samir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Pointers (Part 1) : Computer Science and The Art of Programming, Harvard School of

This lecture introduces pointers in C programming. Pointers contain the memory address of a variable. Pointer variables can be initialized and assigned memory addresses using the address-of operator (&). The indirection operator (*) is used to dereference a pointer and access the value of the variable it points to. Pointer arithmetic allows pointers to traverse through arrays by incrementing/decrementing the memory address. Pointers enable passing arguments by reference and dynamic memory allocation.

Uploaded by

Mohamed Samir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

CS112 – Level 1

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

• They enable programs:


• To simulate call-by-reference
• To pass arrays and strings more conveniently from one
function to another
• To support dynamic data structures, such as linked lists,
queues, stacks and trees.

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

Define a pointer (holds a memory


int * ptr; address):
ptr is a pointer for an integer variable
4
NULL
-Means points to nothing -Defined in
Pointer Initialization <stdio.h>

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 )

Pointers operators: & and * operator

int count, a; aPtr a

int *aPtr; 7

count

a = 7; 7

aPtr = &a; The value of the variable


it points to ..
count = *aPtr;

printf( "\nThe value of count is %d", count);


The value of count is 7
aPtr a
Direct and Indirect Values 1024 84

A pointer variable has two associated values:


• Its direct value, which is referenced by using the name
of the variable, aptr.
In this example, this value is 1024. We can print the
direct value of a pointer variable using %p as the place
holder in printf .
printf (“The value of aPtr is %p", aPtr );

• Its indirect value, which is referenced by using the


indirection operator (*).
So the value of *aptr is 84.
printf (“The value of *aPtr is %d", *aPtr );
Indirection Operator Example
#include <stdio.h>
int main( void ) {
char g = 'z';
char c = 'a';
char *p;
p = &c;
printf("%c\n",*p);
p = &g;
printf("%c\n",*p);
*p = 'K';
printf("%c\n",g);
return 0;
}
Pointer to pointer
3000
ptr var

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

int *p1, *p2; 6

p2

p1 = &x; Both pointers must


point to same type
p2 = p1;
WHY ?

printf ("&x=%p, p1= %p, p2= %p ", &x, p1, p2);


printf ("x=%d, *p1= %d, *p2= %d ", x, *p1, *p2);
&x=1234, p1=1234, p2=1234
x= 6 , *p1= 6, *p2= 6 14
Pointer Arithmetic: Assumes that:
Example 1 int type reserves 4 bytes
Memory
int a[5]; /* a is actually a pointer address
to first element in the array*/ a 3000
aPtr a[0]
int *aPtr;
a[1] 3004
aPtr = a; // note: a= &a[0]
printf ("aPtr = %p\n", aPtr ); a[2] 3008

printf ("++aPtr = %p\n", ++aPtr ); a[3] 3012


printf ("aPtr+2 = %p\n", aPtr+=2 ); 3016
a[4]
printf ("- -aPtr = %p\n", - -aPtr );
aPtr= 3000 Based on
++ aPtr= 3004 pointer type
aPtr+2 = 3012
- -aPtr= 3008 15
Take care
• Array name is a constant pointer, always points to
the beginning of the array
 can’t change it
• Assuming, b is the name of an array:
b+=3 // Compilation error attempt to
// modify the value of array name

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

Number of array elements a[3] 3012


between the 2 pointers
x= p2-p1;
a[4] 3014
printf ("x = %d\n", x );
x=2

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

Notations with Arrays 20


30
40
// See complete code fig. 7.20 p.328-29
int b[ ] = { 10, 20, 30, 40 };
b[ 0 ] = 10
int i; //counter b[ 1 ] = 20
for ( i = 0; i < 4; ++i ) { b[ 2 ] = 30
printf( "b[ %u ] = %d\n", i, b[ i ] ); b[ 3 ] = 40
}
for ( i = 0; i < 4; ++i ) {
printf( "*( b + %u ) = %d\n", i, *( b +i ) ); *( b + 0 ) = 10
} *( b + 1 ) = 20
It doesn’t modify the *( b + 2 ) = 30
array name value *( b + 3 ) = 40
20
b
Using Subscripting and PointerbPtr 10

Notations with Arrays (Cont.) 20


30
int *bPtr = b; 40

// output values using bPtr and array subscript notation


for ( i = 0; i < 4; ++i ) { bptr[ 0 ] = 10
printf( "bPtr[ %u ] = %d\n", i, bPtr[ i ]); bptr[ 1 ] = 20
bptr[ 2 ] = 30
} Pointers can be
bptr[ 3 ] = 40
subscripted like arrays

21
b
Using Subscripting and PointerbPtr 10

Notations with Arrays (Cont.) 20


30
int *bPtr = b; 40

// output values using bPtr and array subscript notation


for ( i = 0; i < 4; ++i ) { *( bPtr + 0 ) = 10
printf( "bPtr[ %u ] = %d\n", i, bPtr[ i ]); *( bPtr + 1 ) = 20
} Pointers can be *( bPtr + 2 ) = 30
subscripted like arrays *( bPtr + 3 ) = 40
// output values using bPtr and pointer/offset notation
for ( i = 0; i < 4; ++i ) {
printf( "*( bPtr + %u ) = %d\n", i, *( bPtr + i) );
}
22
Using Subscripting and Pointerb
Notations with Arrays (Cont.) bPtr 10
20
30
// See complete code fig. 7.20 p.328-29 40
// output values using bPtr and array subscript notation
for ( i = 0; i < 4; ++i ) { bptr[
*( bPtr + 00 )] == 10
10
bptr[
printf( "bPtr[ %u ] = %d\n", i, bPtr[ i ]); *( bPtr + 11 )] == 20
20
} Pointers can be bptr[
*( bPtr + 22 )] == 30
30
subscripted like arrays bptr[
*( bPtr + 33 )] == 40
40

// output values using bPtr and pointer/offset notation


for ( i = 0; i < 4; ++i ) {
printf( "*( bPtr + %u ) = %d\n", i, *(Don’t
bPtr + forget
i) );
} We can say bPtr += i
But We can’t say b += i
Section 7.9 23
Arrays with Pointers
Same as: int a[]
int minimum ( int * a, int minimum (int * a,
int size) int size)
{ {
int min= a[0]; int min= *(a+0);
for (int i=1; i< size;i++) for (int i=1; i< size;i++)
if (a[i]< min) if (*(a+i) < min)
min=a[i]; min=*(a+i);
return min; return min;
} }
Arrays with Pointers: Main
function
#include <stdio.h>
#define SIZE 5
int main ()
{
int m;
int array[ ]={1,2,3,4,5};
m= minimum (array, SIZE);
printf(“The minimum number is %d”, m);
}
Arrays of Pointers
• A common use of an array of pointers is to form
an array of strings
• Ex: const char *suit[ 4 ] =
{ "Hearts", "Diamonds", "Clubs", "Spades" };
..
for (i=0; i<4; i++)
printf(“%s”, suit[i]); // to display the words

Section 7.10 26
Thanks! .. Questions?

You might also like