1 / 49

CSC112 Algorithms and Data Structures

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:

sade-mack
Download Presentation

CSC112 Algorithms and Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC112Algorithms and Data Structures By Muhammad Fayyaz Lecture 5&6 Pointers

  2. 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

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

  4. 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 = #

  5. 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)

  6. 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)

  7. Pointer and Functions • The arguments or parameters to the functions are passed by two ways: • Call by value • Call by reference

  8. 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; }

  9. Reference variables (cont.) • Very useful for calling a function by reference. void newval(float& xnum, float& ynum) { xnum = 89.5; ynum = 99.5; }

  10. 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 *).

  11. 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;

  12. Pointers and Array • Difference between reference and pointer

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

  14. Pointer and Arrays (Cont…) int word [5] = {0,1,2,3,4}; int * ptr = word; • Multi-dimensional array

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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]; }

  20. 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

  21. 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

  22. 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.

  23. 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.

  24. Pointer and Arrays (Cont…) int word [5] = {0,1,2,3,4}; int * ptr = word; • Multi-dimensional array

  25. 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

  26. 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

  27. 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

  28. 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

  29. 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]; }

  30. 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

  31. 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

  32. Structures

  33. Structures • Collection of different record • What is the difference between array and structure? • Example:

  34. Declaration of Structure • Structures are declared by using “struct” keyword. • Structure ends with “semicolon”. • For example: struct { …………… …………… …………… }; Variable declarations

  35. 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

  36. 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.

  37. Array of Structures structrec{ char name[10]; int age; char address[10]; }person[10]; Note: Why we use array of structures?

  38. 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) {………. ………… }

  39. 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) {………. ………… }

  40. 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;

  41. 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; };

  42. 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(); }

  43. Output

  44. 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();

  45. 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;

  46. Pointer of Structures (Cont…)

  47. 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.

  48. 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.

  49. For any query Feel Free to ask

More Related