49
CSC112 Algorithms and Data Structures Lecture 5&6 Pointers By Muhammad Fayyaz

CSC112 Algorithms and Data Structures

Embed Size (px)

DESCRIPTION

CSC112 Algorithms and Data Structures. By Muhammad Fayyaz. Lecture 5&6 Pointers. Contact Information. Instructor : Muhammad Fayyaz (Lecturer ) Department of Computer Sciences Comsats Insititute of Information Technology, Wah Cantt Office Hours: - PowerPoint PPT Presentation

Citation preview

Page 1: CSC112 Algorithms and Data Structures

CSC112Algorithms and Data Structures

Lecture 5&6Pointers

ByMuhammad Fayyaz

Page 2: CSC112 Algorithms and Data Structures

Contact Information

• Instructor: Muhammad Fayyaz (Lecturer)

Department of Computer Sciences

Comsats Insititute of Information Technology, Wah Cantt

• Office Hours:Thuesday, 09:00 am - 12:00 pm, at CS faculty Hall

Page 3: CSC112 Algorithms and Data Structures

Pointers

• A pointer is a variable. It contains the memory address (locations) of another variable which is declared as the pointer type.

Page 4: CSC112 Algorithms and Data Structures

The & and * operator• The & is the address operator. It represents the address

of the variable.• Storing address in pointers

int num = 22;

int * num_addr = #

Page 5: CSC112 Algorithms and Data Structures

Declaration of Pointer• How we use pointer in the expression?

The & operator represents the address of variable. If we want to give the address of variable to another variable then we can write it as:

int a=5;

b=&a; //here b is the variable which contains the address of the variable a.

5 2000

a b

2000 (Memory address)

3000 (Memory address)

Page 6: CSC112 Algorithms and Data Structures

Pointer to Pointerint a=5;

int *b;

int **c;

b=&a;

c=&b;

5

a

2000 (Memory address)

2000

b

3000 (Memory address)

3000

c

4000 (Memory address)

Page 7: CSC112 Algorithms and Data Structures

Pointer and Functions

• The arguments or parameters to the functions are passed by two ways:

1. Call by value

2. Call by reference

Page 8: CSC112 Algorithms and Data Structures

Passing a pointer variable to a function (cont.) 

void newval(float *, float *); // function prototype 

int main() {

float firstnum, secnum; 

cout << "Enter two numbers: "; cin >> firstnum >> secnum;

 

newval(&firstnum, &secnum); // pass the address explicitly !! 

cout << firstnum << secnum << endl; 

return 0;}

 

void newval(float *xnum, float *ynum) { *xnum = 89.5; // dereferencing is required !! *ynum = 99.5;

}

Page 9: CSC112 Algorithms and Data Structures

Reference variables (cont.)

• Very useful for calling a function by reference. 

void newval(float& xnum, float& ynum) {

xnum = 89.5;

ynum = 99.5;

}

Page 10: CSC112 Algorithms and Data Structures

Differences between references and pointers

a) A reference parameter is a constant pointer (after initializing a reference parameter, we cannot change it again).

b) References are dereferenced automatically (no need to use the dereferencing operator *).

Page 11: CSC112 Algorithms and Data Structures

Differences between references and pointers (cont.)

int b; // using reference variables

int& a = b;

a = 10;

 

int b; // using pointers

int *a = &b;

*a = 10;

Page 12: CSC112 Algorithms and Data Structures

• Pointers and Array• Difference between reference and pointer

Page 13: CSC112 Algorithms and Data Structures

Pointer and Arrays• What is array? And its declaration? (previous lectures)

Page 14: CSC112 Algorithms and Data Structures

Pointer and Arrays (Cont…)int word [5] = {0,1,2,3,4}; int * ptr = word;

• Multi-dimensional array

Page 15: CSC112 Algorithms and Data Structures

Pointer and Array

• In case of pointers, 1D array can be defined as:

int array[4]={3,10,11,13};for(int i=0; i<=4; i++){cout<<“Address of array ”<<i<<“\t is”<<&array[i];}

3 10 11 13

array[0] array[1] array[2] array[3]

1000 2000 3000 4000

Page 16: CSC112 Algorithms and Data Structures

Result is:

Address of array 0 is 1000Address of array 1 is 2000Address of array 2 is 3000Address of array 3 is 4000

Page 17: CSC112 Algorithms and Data Structures

Pointer and Array (Cont…)int array[4]={3,10,11,13};int *b;b=array; // also can be written as b=&array[0]

for(int i=0; i<=4; i++){cout<<“Value of array”<<i<<“ is”<<*b;cout<<“Address of array ”<<i<<“\t is”<<b;b=b+1;}

b b+1 b+2 b+3

3 10 11 13

array[0] array[1] array[2] array[3]

1000 1004 1008 1012

1000 1004 1008 1012

Page 18: CSC112 Algorithms and Data Structures

Result is:

Value of array 0 is 3 Address of array 0 is 1000Value of array 1 is 10 Address of array 1 is 1004Value of array 2 is 11 Address of array 2 is 1008Value of array 3 is 13 Address of array 3 is 1012

Page 19: CSC112 Algorithms and Data Structures

Pointer with 2D Arrays

int array[3][2]={ {10,100},

{20,200},

{30,300}

};

int i,j;

for(i=0; i<3; i++)

{

cout<<“Address of array ”<<i<<“ is”<<&array[i];

for(j=0;j<2;j++)

cout<<“Value=”<<array[i][j];

}

Page 20: CSC112 Algorithms and Data Structures

Example:

10 100 20 200 30 300

array[0][0] array[0][1] array[1][0] array[1][1] array[2][0] array[2][1]

1000 1004 1008 1012 1016 1020

Page 21: CSC112 Algorithms and Data Structures

Output

Address of array 0 is 1000Value=10Value=100Address of array 1 is 1008Value=20Value=200Address of array 2 is 1016Value=30Value=300

Page 22: CSC112 Algorithms and Data Structures

Dynamic Array Allocation

• To avoid wasting memory, array allocation or de-allocation can take place at run time

• To allocate memory, we need to use the new operatorReserves the number of bytes requested by the declaration. Returns the address of the first reserved location or NULL if sufficient memory is not available.

• To de-allocate memory (which has previously been allocated using the new operator) we need to use the delete operator.

Releases a block of bytes previously reserved. The address of the first reserved location is passed as an argument to delete.

Page 23: CSC112 Algorithms and Data Structures

Uses of Pointers

1. Pointers are used for saving memory space

2. Use of pointer, assigns the memory space and also releases it. This concept helps in making the best use of the available memory (dynamic memory allocation).

3. With pointer, data manipulation is done with address, so the execution time is faster.

Page 24: CSC112 Algorithms and Data Structures

Pointer and Arrays (Cont…)int word [5] = {0,1,2,3,4}; int * ptr = word;

• Multi-dimensional array

Page 25: CSC112 Algorithms and Data Structures

Pointer and Array

• In case of pointers, 1D array can be defined as:

int array[4]={3,10,11,13};for(int i=0; i<=4; i++){cout<<“Address of array ”<<i<<“\t is”<<&array[i];}

3 10 11 13

array[0] array[1] array[2] array[3]

1000 2000 3000 4000

Page 26: CSC112 Algorithms and Data Structures

Result is:

Address of array 0 is 1000Address of array 1 is 2000Address of array 2 is 3000Address of array 3 is 4000

Page 27: CSC112 Algorithms and Data Structures

Pointer and Array (Cont…)int array[4]={3,10,11,13};int *b;b=array; // also can be written as b=&array[0]

for(int i=0; i<=4; i++){cout<<“Value of array”<<i<<“ is”<<*b;cout<<“Address of array ”<<i<<“\t is”<<b;b=b+1;}

b b+1 b+2 b+3

3 10 11 13

array[0] array[1] array[2] array[3]

1000 1004 1008 1012

1000 1004 1008 1012

Page 28: CSC112 Algorithms and Data Structures

Result is:

Value of array 0 is 3 Address of array 0 is 1000Value of array 1 is 10 Address of array 1 is 1004Value of array 2 is 11 Address of array 2 is 1008Value of array 3 is 13 Address of array 3 is 1012

Page 29: CSC112 Algorithms and Data Structures

Pointer with 2D Arrays

int array[3][2]={ {10,100},

{20,200},

{30,300}

};

int i,j;

for(i=0; i<3; i++)

{

cout<<“Address of array ”<<i<<“ is”<<&array[i];

for(j=0;j<2;j++)

cout<<“Value=”<<array[i][j];

}

Page 30: CSC112 Algorithms and Data Structures

Example:

10 100 20 200 30 300

array[0][0] array[0][1] array[1][0] array[1][1] array[2][0] array[2][1]

1000 1004 1008 1012 1016 1020

Page 31: CSC112 Algorithms and Data Structures

Output

Address of array 0 is 1000Value=10Value=100Address of array 1 is 1008Value=20Value=200Address of array 2 is 1016Value=30Value=300

Page 32: CSC112 Algorithms and Data Structures

Structures

Page 33: CSC112 Algorithms and Data Structures

Structures• Collection of different record

• What is the difference between array and structure?

• Example:

Page 34: CSC112 Algorithms and Data Structures

Declaration of Structure• Structures are declared by using “struct” keyword. • Structure ends with “semicolon”.

• For example: struct {………………………………………};

Variable declarations

Page 35: CSC112 Algorithms and Data Structures

Declaration of Structure• Structure can be declared as by using tagname or structure

type variable or both.struct tagname{};

struct{} var;

struct tagname{

} var;

“var” is a structure type variable

Page 36: CSC112 Algorithms and Data Structures

Note• Tagname can be helpful for re-use of any structure.

• “.” or dot operator is used to access any structure.

• The structure can be declared within main or outside the main block. But the preference would be the structure should declare outside the main block.

Page 37: CSC112 Algorithms and Data Structures

Array of Structures

struct rec{char name[10];int age;char address[10];}person[10];

Note: Why we use array of structures?

Page 38: CSC112 Algorithms and Data Structures

Passing Structure to Function• As in functions, we take the variables as actual parameter and

pass them in the same data type in formal parameter. Similarly we can pass the structure variable in the functions.

struct rec{char name[20];int age;}var;

int main(){struct rec var;……………function(struct rec var);……………} // end of main

function(struct rec var){……….…………}

Page 39: CSC112 Algorithms and Data Structures

Passing Array of Structures to Functions

• Why we are passing array of structures to functions?

• For example:struct rec{char name[20];int age;};

int main(){struct rec emp[10];……………function(struct emp);……………} // end of main

function(struct rec person){……….…………}

Page 40: CSC112 Algorithms and Data Structures

Structure within Structure

struct rec{char name[20];int age;

struct dob{int day;int month;int year;}birthday;

}emp;

To access month –struct emp.birthday.month;

Page 41: CSC112 Algorithms and Data Structures

Example#include<iostream.h>

#include<stdlib.h>

#include<string.h>

#include<conio.h>

struct rec{

char name[10];

struct

{

int date;

int month;

int year;

}dob;

};

int main(){ rec emp[10]; int i=0,num; char ch;cout<<"Enter name and age of employees"<<endl;cout<<endl<<endl;cout<<"Enter the number of employees record : "<<endl;cin>>num;

do{cout<<"Enter the name of an employee :"<<endl;cin>>emp[i].name;

cout<<"Enter joining date,month and year of an employee "<<i<<" is:"<<endl;cin>>emp[i].dob.date;cout<<endl;cin>>emp[i].dob.month;cout<<endl;cin>>emp[i].dob.year;i++; } while(i<num);

Page 42: CSC112 Algorithms and Data Structures

Example (Cont…)

for(int j=0; j<i; j++){

cout<<"The name of "<<j<<" is "<<emp[j].name<<endl;cout<<"The Joining Information of an employee is "<<j<<" date

"<<emp[j].dob.date<<" month "<<emp[j].dob.month<<" year "<<emp[j].dob.year<<endl;

}getch();}

Page 43: CSC112 Algorithms and Data Structures

Output

Page 44: CSC112 Algorithms and Data Structures

Pointer to Structures• Simple structure or array of structure can be invoked by using

statement:

rec emp[10]; or rec emp;

• While structure pointer is declared as:struct rec {char name[10];int reg;}*q;

• In main it is invoked by using statement: q = new rec();

Page 45: CSC112 Algorithms and Data Structures

Pointer of Structures (Cont…)• #include<iostream>• #include<conio.h>• using namespace std;• struct rec {• char name[10];• int reg;• }*q[10];

• int main()• {• int i=0,num;• for(int j=0; j<10; j++)• q[j] = new rec();

• cout<<"Enter the number of record to use"<<endl;

• cin>>num;• cout<<"Enter the name and reg of

student"<<endl;

do{cout<<"Enter name. \n";cin>>q[i]->name;cout<<"Enter reg.\n";cin>>q[i]->reg;i++;}while(i<num);

for(int j=0; j<num; j++){cout<<"Name: "<<q[j]->name<<" Reg.Num "<<q[j]->reg<<endl;}getch();}

Page 46: CSC112 Algorithms and Data Structures

Pointer of Structures (Cont…)

Page 47: CSC112 Algorithms and Data Structures

Dynamic Array Allocation

• To avoid wasting memory, array allocation or de-allocation can take place at run time

• To allocate memory, we need to use the new operatorReserves the number of bytes requested by the declaration. Returns the address of the first reserved location or NULL if sufficient memory is not available.

• To de-allocate memory (which has previously been allocated using the new operator) we need to use the delete operator.

Releases a block of bytes previously reserved. The address of the first reserved location is passed as an argument to delete.

Page 48: CSC112 Algorithms and Data Structures

Uses of Pointers

1. Pointers are used for saving memory space

2. Use of pointer, assigns the memory space and also releases it. This concept helps in making the best use of the available memory (dynamic memory allocation).

3. With pointer, data manipulation is done with address, so the execution time is faster.

Page 49: CSC112 Algorithms and Data Structures

For any query Feel Free to ask