Upcoming SlideShare
×

# Lec 38.39 - pointers

209 views

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
209
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
9
0
Likes
0
Embeds 0
No embeds

No notes for slide
• Student Book
• Student Book
• ### Lec 38.39 - pointers

1. 1. Chapter: 10 Pointers Lecture: 38 and 39 Date: 18.10.2012
2. 2. Accessing Addressesint main(){ int IntVar1 = 25; int IntVar2 = 11; int* ptr; //pointer to integers ptr = &IntVar1; //pointer points to IntVar1 cout << ptr << endl //print the address of IntVar1 ptr = &IntVar2 cout << ptr << endl //print the address of IntVar2 getch(); return 0; }
3. 3. Accessing Contentsint main(){ int IntVar1 = 25; int IntVar2 = 11; int* ptr; //pointer to integers ptr = &IntVar1; //pointer points to IntVar1 cout << *ptr << endl //print the content of IntVar1 ptr = &IntVar2 cout << *ptr << endl //print the content of IntVar2 getch(); return 0; }
4. 4. Array Accessing Using Indexint main(){int intarray[5] = { 31, 54, 77, 52, 93 };for(int j=0; j<5; j++) cout << intarray[j] << endl;getch();return 0;}
5. 5. Array Accessing Using Pointersint main(){int intarray[5] = { 31, 54, 77, 52, 93 };for(int j=0; j<5; j++) cout << *( intarray + j ) << endl;getch();return 0;}
6. 6. Counting by Integers - Arrays
7. 7. Passing Arguments to Functions Arguments can be passed to functions in three different ways: (i) by value, (ii) by reference, and (iii) by pointers A function can change the values in a calling function if the arguments are passed by a reference or by a pointer.
8. 8. Pass-by-Referencevoid centimize(double& );int main(){ double var = 2.5; centimize(var); cout << var << endl;getch(); return 0; }void centimize(double& v){ v = v * 100; }
9. 9. Pass-by-Pointervoid centimize(double* );int main(){ double var = 2.5; centimize(&var); cout << var << endl;getch(); return 0; }void centimize(double* ptrd){ *ptrd = *ptrd * 100; }
10. 10. Pointer Passed to Function
11. 11. Passing Arrays to Functionconst int MAX = 5;void centimize(double*);int main(){ double varray[MAX] = { 10.0, 43.1, 95.9, 59.7, 87.3 }; centimize(varray); for(int j=0; j<MAX; j++) cout << varray[j] << endl;getch(); return 0; }void centimize(double* ptrd){ for(int j=0; j<MAX; j++) *ptrd++ = *ptrd * 2.54; } //*ptrd++ = *(ptrd++)
12. 12. Pointer Passed to Function
13. 13. Assignment # 02(i) Write a note on sorting with an example.(ii) What is bubble sort? Write a program implementing bubble sort using pointers.
14. 14. Assignment # 02(i) Write a note on sorting with an example(ii) What is bubble sort? Write a program implementing bubble sort using pointers.Viva voce for the first and second assignment is scheduled to be conducted on 24th October.