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

Lec #2

Uploaded by

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

Lec #2

Uploaded by

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

DATA STRUCTURES & ALGORITHMS

CS-201

LECTURE #2
Pointers

MAHMOOD AHMAD
Topics covered

• Introduction to Addressing Variables


• Pointer types
• Address Operator &
• Dereferencing Operator *
• Reference Variables
• Pointers as function arguments
• Pointer to Arrays

Mahmood Ahmad
Addressing variables

• Every variable residing in memory has an address!


• What doesn’t have an address?
 Register variables
 Constants / literals
 Expressions (unless result is a variable)

Mahmood Ahmad
Addressing variables
• Each variable is assigned a memory slot (the size depends on the data type) and
the variable’s data is stored there

• Memory address: 1020 1024 1032

… … … …
a

Variable a’s value, i.e., 100, is


int a = 100;
stored at memory location 1024

Mahmood Ahmad
Addressing variables
• A pointer is a variable used to store the address of a memory cell.
• We can use the pointer to reference this memory cell

Memory address: 1020 1024 1032

… … 100 … 1024 …
integer
pointer

Mahmood Ahmad
Pointer Types

• Pointer
 C++ has pointer types for each type of object
 Pointers to int objects
 Pointers to char objects
 Pointers to user-defined objects
• (e.g., RationalNumber)
 Even pointers to pointers
 Pointers to pointers to int objects

Mahmood Ahmad
Pointer Types

• Q: Why is it important to declare the type of the variable that a pointer


points to? Aren’t all addresses of the same length?

• A: It’s true that all addresses are of the same length, however when we
perform an operation of the type “p++” where “p” is a pointer variable, for
this operation to make sense the compiler needs to know the data type
of the variable “p” points to. If “p” is a character pointer then “p++” will
increment “p” by one byte (typically), if “p” were an integer pointer its
value on “p++” would be incremented by 2 bytes (typically).

Mahmood Ahmad
Pointer Types

Declaration of Pointer variables


type* pointer_name;
//or
type *pointer_name;
where type is the type of data pointed to (e.g. int, char, double)

Examples:
int *n;
RationalNumber *r;
int **p; // pointer to pointer

Mahmood Ahmad
Address Operator &
• The "address of " operator (&) gives the memory address of the
variable
 Usage: &variable_name

• Memory address: 1020 1024

… … … …
• a
• int a = 100;
• //get the value,
• cout << a; //prints 100
• //get the memory address
• cout << &a; //prints 1024

Mahmood Ahmad
Address Operator &
Memory address: 1020 1024 1032

… … … …
a b
#include <iostream>
Result is:
using namespace std;
The address of a is:
void main(){
1020
int a, b; The address of b is:
a = 88; 1024
b = 100;
cout << "The address of a is: " << &a << endl;
cout << "The address of b is: " << &b << endl;
}
Mahmood Ahmad
Address Operator &

Memory address: 1020 1024 1032

… 88 100 … 1024 …
a p
int a = 100;
int *p = &a;
cout << a << " " << &a <<endl;
Result is:
cout << p << " " << &p <<endl;
100 1024
1024 1032

Mahmood Ahmad
Address Operator &

What is the
output?

58 58 58

Mahmood Ahmad
Dereferencing Operator *
• We can access to the value stored in the variable
pointed to by using the dereferencing operator (*),

Memory address: 1020 1024 1032

… 88 100 … 1024 …
int a = 100; a p
int *p = &a; Result is:
cout << a << endl; 100
cout << &a << endl; 1024
cout << p << " " << *p << endl; 1024 100
cout << &p << endl; 1032

Mahmood Ahmad
Don’t get confused
• Declaring a pointer means only that it is a pointer:
int *p;
• Don’t be confused with the dereferencing operator,
which is also written with an asterisk (*). They are
simply two different tasks represented with the same
sign
int a = 100, b = 88, c = 8;
int *p1 = &a, *p2, *p3 = &c;
p2 = &b; // p2 points to b
p2 = p1; // p2 points to a Result is:
b = *p3; //assign c to b 888
*p2 = *p3; //assign c to a
cout << a << b << c;

Mahmood Ahmad
A Pointer Example
The code

void doubleIt(int x, int * p)


Memory Layout
Box diagram
{
main
*p = 2 * x;
} p
a 16 8192
int main() (8200) doubleIt
{
int a = 16; x 9
(8196)
doubleIt(9, &a);
return 0; doubleIt a 16 main
(8192)
}

x 9

a gets 18 p

Mahmood Ahmad
Another Pointer Example
#include <iostream>
using namespace std;
int main (){
int value1 = 5, value2 = 15 ;
int *p1, *p2;
p1 = &value1; // p1 = address of value1
p2 = &value2; // p2 = address of value2
*p1 = 10; // value pointed to by p1=10
*p2 = *p1; // value pointed to by p2= value
// pointed to by p1
p1 = p2; // p1 = p2 (pointer value copied)
*p1 = 20; // value pointed to by p1 = 20
cout << "value1==" << value1 << "/ value2==" << value2;
return 0;
}
Mahmood Ahmad
Reference Variables
A reference is an additional name to an existing
memory location
Pointer: Reference:

x 9 x
9
ref

ref

int x = 9;
int x=9;
int &ref = x;
int *ref;
ref = &x;

Mahmood Ahmad
Reference Variables
• A reference variable serves as an alternative name for an object

int m = 10;
int &j = m; // j is a reference variable
cout << “value of m = “ << m << endl;

j = 18;
cout << “value of m = “ << m << endl;

Output
value of m = 10
value of m = 18

Mahmood Ahmad
Reference Variables
• A reference variable always refers to the same object. Assigning a reference variable
with a new value actually changes the value of the referred object.
• Reference variables are commonly used for parameter passing to a function

void swap(int& first, int& second)


{ swap code}
void main(){
int a = 2, b = 3;
swap(a, b); Output
cout << a << " " << b;
3 2
}

Mahmood Ahmad
Traditional Pointer Usage
void IndirectSwap(char *Ptr1, char *Ptr2){
char temp = *Ptr1;
*Ptr1 = *Ptr2;
*Ptr2 = temp;
}
int main() {
char a = 'y';
char b = 'n';
IndirectSwap(&a, &b); Output
cout << a << b << endl; n
return 0; y
}

Mahmood Ahmad
Pass by Reference
void IndirectSwap(char& y, char& z) {
char temp = y;
y = z;
z = temp;
}
int main() {
char a = 'y';
char b = 'n';
Output
IndirectSwap(a, b); n
cout << a <<endl<< b << endl; y
return 0;

Mahmood Ahmad
Pass by Value
What is the Problem ??

void IndirectSwap(char y, char z) {


char temp = y;
y = z;
z = temp;
}
int main() {
char a = 'y';
Output
char b = 'n'; y
IndirectSwap(a, b); n
cout << a <<endl<< b << endl;
return 0;
}
Mahmood Ahmad
Pointers and Arrays
• The name of an array points only to the first element not the
whole array.

1000

1004

1008

1012

1016

Mahmood Ahmad
Array Name is a pointer constant
#include <iostream>
using namespace std;

void main (){


int a[5];
cout << "Address of a[0]: " << &a[0] << endl
<< "Name as pointer: " << a << endl;
}

Result:
Address of a[0]:
0x0065FDE4
Name as pointer:
0x0065FDE4

Mahmood Ahmad
Dereferencing An Array Name

This element is
called a[0] or
*a
#include <iostream>
a[0] 2 using namespace std;
a void main(){
a[1] 4 int a[5] = {2,4,6,8,22};
a[2] 6 cout << *a << " "
<< a[0];
a[3] 8 } //main
a[4] 22
Output
a 2
2

Mahmood Ahmad
Array Names as Pointers
• To access an array, any pointer to the first element can be
used instead of the name of the array.
We could replace *p by *a

#include <iostream>
p using namespace std;
a
void main(){
a[0] 2 int a[5] = {2,4,6,8,22};
a[1] 4 int *p = a;
a[2] 6 cout << a[0] << " "
a[3] 8 << *p;
} Output
a[4] 22
2
a 2

Mahmood Ahmad
Multiple Array Pointers
• Both a and p are pointers to the same array.

#include <iostream>
a[0] using namespace std;
a[0] void main(){
2 p
4 int a[5] = {2,4,6,8,22};
a[1] 4 int *p = &a[1];
a[2] 6 cout << a[0] << " " Output
<< p[-1] << " “<< *( p-1) 2 2 2
a[3] 8 p[0]
cout << a[1] << " " 4 4 4
a[4] 22 << p[0] << " " << *p;
}

Mahmood Ahmad
Pointer Arithmetic
• Given a pointer p, p+n refers to the element that is offset
from p by n positions.

a 2 p - 1

a + 1 4 p

a + 2 6 p + 1
a + 3 8 p + 2
a + 4 22 p + 3

Mahmood Ahmad
Dereferencing Array Pointers

*(a+n) is identical to a[n]


Note: flexible pointer syntax
Mahmood Ahmad
Array of Pointers & Pointers to Array
a
p
b
c

An array of Pointers A pointer to an array


int a = 1, b = 2, c = 3; int list[5] = {9, 8, 7, 6, 5};
int *p[5]; int *p;
p[0] = &a; P = list;//points to 1st entry
p[1] = &b; P = &list[0];//points to 1st entry
p[2] = &c; P = &list[1];//points to 2nd entry
P = list + 1; //points to 2nd entry

Mahmood Ahmad
NULL pointer
• NULL is a special value that indicates an empty pointer
• If you try to access a NULL pointer, you will get an error
int *p;
p = 0;
cout << p << endl; //prints 0
cout << &p << endl;//prints address of p
cout << *p << endl;//Error!

Mahmood Ahmad
Example
• char* a = “Hello”;
 a -> gives address of ‘H’
 *a -> gives ‘H’
 a[0] -> gives ‘H’
 a++ -> gives address of ‘e’
 *a++ -> gives ‘e’
 a = &b; where b is another char variable is
perfectly LEGAL. However “char a[100];” “a =&b;”
where b is another char variable is ILLEGAL.

Mahmood Ahmad
Pointer to 2-Dimensional Arrays
table[i] =
table
&table[i][0]
refers to
table + 1 table[ 0] or *( table + 0 ) the address
What is
of the ith
*table row
? table + 2 table[ 1] or *( table + 1 )
address

table[ 2] or *( table + 2 )
int table[3][4] = {{1,2,3,4}, *(table[i]+j)
What is {5,6,7,8},{9,10,11,12}}; = table[i][j]
**table
? for(int i=0; i<3; i++){
1 for(int j=0; j<4; j++)
cout << *(*(table+i)+j);
cout << endl;

Mahmood Ahmad
Pointers as Function Parameters

• Pointer to a constant: cannot change the value that is pointed


at
• const int *p= &a;
• Cannot change value of a but can be re-assigned
• Constant pointer: address in pointer cannot change once
pointer is initialized
• int * const p1=&b;
• can change value of b but cannot be re- assigned

Mahmood Ahmad
Mahmood Ahmad

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy