Upload
sade-mack
View
36
Download
7
Tags:
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
CSC112Algorithms and Data Structures
Lecture 5&6Pointers
ByMuhammad Fayyaz
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
Pointers
• A pointer is a variable. It contains the memory address (locations) of another variable which is declared as the pointer type.
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 = #
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)
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)
Pointer and Functions
• The arguments or parameters to the functions are passed by two ways:
1. Call by value
2. Call by reference
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;
}
Reference variables (cont.)
• Very useful for calling a function by reference.
void newval(float& xnum, float& ynum) {
xnum = 89.5;
ynum = 99.5;
}
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 *).
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;
• Pointers and Array• Difference between reference and pointer
Pointer and Arrays• What is array? And its declaration? (previous lectures)
Pointer and Arrays (Cont…)int word [5] = {0,1,2,3,4}; int * ptr = word;
• Multi-dimensional array
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
Result is:
Address of array 0 is 1000Address of array 1 is 2000Address of array 2 is 3000Address of array 3 is 4000
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
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
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];
}
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
Output
Address of array 0 is 1000Value=10Value=100Address of array 1 is 1008Value=20Value=200Address of array 2 is 1016Value=30Value=300
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.
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.
Pointer and Arrays (Cont…)int word [5] = {0,1,2,3,4}; int * ptr = word;
• Multi-dimensional array
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
Result is:
Address of array 0 is 1000Address of array 1 is 2000Address of array 2 is 3000Address of array 3 is 4000
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
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
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];
}
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
Output
Address of array 0 is 1000Value=10Value=100Address of array 1 is 1008Value=20Value=200Address of array 2 is 1016Value=30Value=300
Structures
Structures• Collection of different record
• What is the difference between array and structure?
• Example:
Declaration of Structure• Structures are declared by using “struct” keyword. • Structure ends with “semicolon”.
• For example: struct {………………………………………};
Variable declarations
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
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.
Array of Structures
struct rec{char name[10];int age;char address[10];}person[10];
Note: Why we use array of 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){……….…………}
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){……….…………}
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;
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);
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();}
Output
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();
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();}
Pointer of Structures (Cont…)
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.
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.
For any query Feel Free to ask