490 likes | 619 Views
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:
E N D
CSC112Algorithms and Data Structures By Muhammad Fayyaz Lecture 5&6 Pointers
Contact Information • Instructor: Muhammad Fayyaz(Lecturer) Department of Computer Sciences ComsatsInsititute of Information Technology, WahCantt • 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. b a 5 2000 2000 (Memory address) 3000 (Memory address)
Pointer to Pointer int a=5; int *b; int **c; b=&a; c=&b; b c a 5 2000 3000 3000 (Memory address) 4000 (Memory address) 2000 (Memory address)
Pointer and Functions • The arguments or parameters to the functions are passed by two ways: • Call by value • 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 reference parameter is a constant pointer(after initializing a reference parameter, we cannot change it again). • 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(inti=0; i<=4; i++) { cout<<“Address of array ”<<i<<“\t is” <<&array[i]; } array[0]array[1]array[2]array[3] 3 10 11 13 1000 2000 3000 4000
Result is: Address of array 0 is 1000 Address of array 1 is 2000 Address of array 2 is 3000 Address 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(inti=0; i<=4; i++) { cout<<“Value of array”<<i<<“ is”<<*b; cout<<“Address of array ”<<i<<“\t is” <<b; b=b+1; } array[0]array[1]array[2]array[3] 3 10 11 13 1000 1004 1008 1012 1000 1004 1008 1012 bb+1 b+2 b+3
Result is: Value of array 0 is 3 Address of array 0 is 1000 Value of array 1 is 10 Address of array 1 is 1004 Value of array 2 is 11 Address of array 2 is 1008 Value 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} }; inti,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: array[0][0] array[0][1] array[1][0] array[1][1] array[2][0] array[2][1] 10 100 20 200 30 300 1000 1004 1008 1012 1016 1020
Output Address of array 0 is 1000 Value=10 Value=100 Address of array 1 is 1008 Value=20 Value=200 Address of array 2 is 1016 Value=30 Value=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 operator Reserves 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 • Pointers are used for saving memory space • 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). • 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(inti=0; i<=4; i++) { cout<<“Address of array ”<<i<<“\t is” <<&array[i]; } array[0]array[1]array[2]array[3] 3 10 11 13 1000 2000 3000 4000
Result is: Address of array 0 is 1000 Address of array 1 is 2000 Address of array 2 is 3000 Address 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(inti=0; i<=4; i++) { cout<<“Value of array”<<i<<“ is”<<*b; cout<<“Address of array ”<<i<<“\t is” <<b; b=b+1; } array[0]array[1]array[2]array[3] 3 10 11 13 1000 1004 1008 1012 1000 1004 1008 1012 bb+1 b+2 b+3
Result is: Value of array 0 is 3 Address of array 0 is 1000 Value of array 1 is 10 Address of array 1 is 1004 Value of array 2 is 11 Address of array 2 is 1008 Value 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} }; inti,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: array[0][0] array[0][1] array[1][0] array[1][1] array[2][0] array[2][1] 10 100 20 200 30 300 1000 1004 1008 1012 1016 1020
Output Address of array 0 is 1000 Value=10 Value=100 Address of array 1 is 1008 Value=20 Value=200 Address of array 2 is 1016 Value=30 Value=300
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. structtagname{ }; struct{ } var; structtagname{ } 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 structrec{ 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. structrec{ char name[20]; int age; }var; int main() { structrecvar; …………… function(structrecvar); …………… } // end of main function(structrecvar) {………. ………… }
Passing Array of Structures to Functions • Why we are passing array of structures to functions? • For example: structrec{ char name[20]; int age; }; int main() { structrecemp[10]; …………… function(structemp); …………… } // end of main function(structrec person) {………. ………… }
Structure within Structure structrec{ char name[20]; int age; struct dob{ int day; int month; int year; }birthday; }emp; To access month – structemp.birthday.month;
Example • int main() • { • recemp[10]; • inti=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); #include<iostream.h> #include<stdlib.h> #include<string.h> #include<conio.h> structrec{ char name[10]; struct { int date; int month; int year; }dob; };
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(); }
Pointer to Structures • Simple structure or array of structure can be invoked by using statement: recemp[10]; or recemp; • While structure pointer is declared as: structrec { char name[10]; intreg; }*q; • In main it is invoked by using statement: q = new rec();
Pointer of Structures (Cont…) 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(); } • #include<iostream> • #include<conio.h> • using namespace std; • structrec { • char name[10]; • intreg; • }*q[10]; • int main() • { • inti=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;
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 operator Reserves 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 • Pointers are used for saving memory space • 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). • With pointer, data manipulation is done with address, so the execution time is faster.