Lec 38.39 - pointers
-
Upload
princess-sam -
Category
Documents
-
view
84 -
download
0
Transcript of Lec 38.39 - pointers
![Page 1: Lec 38.39 - pointers](https://reader036.fdocuments.in/reader036/viewer/2022062513/555e33efd8b42aad058b4629/html5/thumbnails/1.jpg)
PointersPointers
Chapter: Chapter: 1010
Lecture: 38 and 39Lecture: 38 and 39
Date: 18.10.2012Date: 18.10.2012
![Page 2: Lec 38.39 - pointers](https://reader036.fdocuments.in/reader036/viewer/2022062513/555e33efd8b42aad058b4629/html5/thumbnails/2.jpg)
Accessing AddressesAccessing Addressesint main()int main()
{ int IntVar1 = 25; { int IntVar1 = 25;
int IntVar2 = 11;int IntVar2 = 11;
int* ptr;int* ptr; //pointer to integers//pointer to integers
ptr = &IntVar1; ptr = &IntVar1; //pointer points to IntVar1//pointer points to IntVar1
cout << ptr << endl cout << ptr << endl //print the address of //print the address of IntVar1IntVar1
ptr = &IntVar2 ptr = &IntVar2
cout << ptr << endl cout << ptr << endl //print the address of //print the address of IntVar2IntVar2
getch();getch();
return 0; }return 0; }
![Page 3: Lec 38.39 - pointers](https://reader036.fdocuments.in/reader036/viewer/2022062513/555e33efd8b42aad058b4629/html5/thumbnails/3.jpg)
Accessing ContentsAccessing Contentsint main()int main()
{ int IntVar1 = 25; { int IntVar1 = 25;
int IntVar2 = 11;int IntVar2 = 11;
int* ptr;int* ptr; //pointer to integers//pointer to integers
ptr = &IntVar1;ptr = &IntVar1; //pointer points to IntVar1//pointer points to IntVar1
cout << cout << *ptr *ptr << endl << endl //print the content of //print the content of IntVar1IntVar1
ptr = &IntVar2 ptr = &IntVar2
cout << cout << *ptr *ptr << endl << endl //print the content of //print the content of IntVar2IntVar2
getch();getch();
return 0; }return 0; }
![Page 4: Lec 38.39 - pointers](https://reader036.fdocuments.in/reader036/viewer/2022062513/555e33efd8b42aad058b4629/html5/thumbnails/4.jpg)
Array Accessing Using IndexArray Accessing Using Index
int main()int main()
{ {
int intarray[5] = { 31, 54, 77, 52, 93 };int intarray[5] = { 31, 54, 77, 52, 93 };
for(int j=0; j<5; j++)for(int j=0; j<5; j++)
cout << intarray[j] << endl; cout << intarray[j] << endl;
getch();getch();
return 0;return 0;
}}
![Page 5: Lec 38.39 - pointers](https://reader036.fdocuments.in/reader036/viewer/2022062513/555e33efd8b42aad058b4629/html5/thumbnails/5.jpg)
Array Accessing Using Array Accessing Using PointersPointers
int main()int main()
{ {
int intarray[5] = { 31, 54, 77, 52, 93 };int intarray[5] = { 31, 54, 77, 52, 93 };
for(int j=0; j<5; j++) for(int j=0; j<5; j++)
cout << cout << *( intarray + j )*( intarray + j ) << endl; << endl;
getch();getch();
return 0;return 0;
}}
![Page 6: Lec 38.39 - pointers](https://reader036.fdocuments.in/reader036/viewer/2022062513/555e33efd8b42aad058b4629/html5/thumbnails/6.jpg)
Counting by Integers - Counting by Integers - ArraysArrays
![Page 7: Lec 38.39 - pointers](https://reader036.fdocuments.in/reader036/viewer/2022062513/555e33efd8b42aad058b4629/html5/thumbnails/7.jpg)
Passing Arguments to Passing Arguments to FunctionsFunctions
Arguments can be passed to functions in three Arguments can be passed to functions in three different ways: (i) by value, (ii) by reference, and (iii) different ways: (i) by value, (ii) by reference, and (iii) by pointersby pointers
A function can change the values in a calling function A function can change the values in a calling function if the arguments are passed by a reference or by a if the arguments are passed by a reference or by a pointer.pointer.
![Page 8: Lec 38.39 - pointers](https://reader036.fdocuments.in/reader036/viewer/2022062513/555e33efd8b42aad058b4629/html5/thumbnails/8.jpg)
Pass-by-ReferencePass-by-Referencevoid centimize(double& );void centimize(double& );
int main()int main()
{ double var = 2.5; { double var = 2.5;
centimize(var); centimize(var);
cout << var << endl;cout << var << endl;
getch(); return 0; }getch(); return 0; }
void centimize(double& v)void centimize(double& v)
{ v = v * 100; }{ v = v * 100; }
![Page 9: Lec 38.39 - pointers](https://reader036.fdocuments.in/reader036/viewer/2022062513/555e33efd8b42aad058b4629/html5/thumbnails/9.jpg)
Pass-by-PointerPass-by-Pointervoid centimize(doublevoid centimize(double** ); );
int main()int main()
{ double var = 2.5; { double var = 2.5;
centimize(centimize(&&var); var);
cout << var << endl;cout << var << endl;
getch(); return 0; }getch(); return 0; }
void centimize(doublevoid centimize(double** ptrd) ptrd)
{ *ptrd = *ptrd * 100; }{ *ptrd = *ptrd * 100; }
![Page 10: Lec 38.39 - pointers](https://reader036.fdocuments.in/reader036/viewer/2022062513/555e33efd8b42aad058b4629/html5/thumbnails/10.jpg)
Pointer Passed to FunctionPointer Passed to Function
![Page 11: Lec 38.39 - pointers](https://reader036.fdocuments.in/reader036/viewer/2022062513/555e33efd8b42aad058b4629/html5/thumbnails/11.jpg)
Passing Arrays to FunctionPassing Arrays to Functionconst int MAX = 5;const int MAX = 5;
void centimize(double*); void centimize(double*);
int main()int main()
{ double varray[MAX] = { 10.0, 43.1, 95.9, 59.7, { double varray[MAX] = { 10.0, 43.1, 95.9, 59.7, 87.3 };87.3 };
centimize(varray); centimize(varray);
for(int j=0; j<MAX; j++) for(int j=0; j<MAX; j++)
cout << varray[j] << endl;cout << varray[j] << endl;
getch(); return 0; }getch(); return 0; }
void centimize(double* ptrd)void centimize(double* ptrd)
{ for(int j=0; j<MAX; j++){ for(int j=0; j<MAX; j++)
*ptrd++ = *ptrd * 2.54; } *ptrd++ = *ptrd * 2.54; } //*ptrd++ = //*ptrd++ = *(ptrd++)*(ptrd++)
![Page 12: Lec 38.39 - pointers](https://reader036.fdocuments.in/reader036/viewer/2022062513/555e33efd8b42aad058b4629/html5/thumbnails/12.jpg)
Pointer Passed to FunctionPointer Passed to Function
![Page 13: Lec 38.39 - pointers](https://reader036.fdocuments.in/reader036/viewer/2022062513/555e33efd8b42aad058b4629/html5/thumbnails/13.jpg)
Assignment Assignment # 02# 02
(i)(i) Write a note on sorting with an example.Write a note on sorting with an example.
(ii)(ii) What is bubble sort? Write a program What is bubble sort? Write a program implementing bubble sort using pointers.implementing bubble sort using pointers.
![Page 14: Lec 38.39 - pointers](https://reader036.fdocuments.in/reader036/viewer/2022062513/555e33efd8b42aad058b4629/html5/thumbnails/14.jpg)
Assignment Assignment # 02# 02
(i)(i) Write a note on sorting with an exampleWrite a note on sorting with an example
(ii)(ii) What is bubble sort? Write a program What is bubble sort? Write a program implementing bubble sort using pointers.implementing bubble sort using pointers.
Viva voce for the first and second Viva voce for the first and second assignment is scheduled to be assignment is scheduled to be conducted on 24conducted on 24thth October. October.