SlideShare a Scribd company logo
Chapter: 10


               Pointers
              Lecture: 38 and 39
               Date: 18.10.2012
Accessing Addresses
int 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; }
Accessing Contents
int 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; }
Array Accessing Using Index

int main()
{
int intarray[5] = { 31, 54, 77, 52, 93 };
for(int j=0; j<5; j++)
        cout << intarray[j] << endl;

getch();
return 0;
}
Array Accessing Using Pointers

int main()
{
int intarray[5] = { 31, 54, 77, 52, 93 };
for(int j=0; j<5; j++)
        cout << *( intarray + j ) << endl;

getch();
return 0;
}
Counting by Integers - Arrays
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.
Pass-by-Reference
void centimize(double& );

int main()
{ double var = 2.5;
   centimize(var);
   cout << var << endl;
getch(); return 0; }

void centimize(double& v)
{ v = v * 100; }
Pass-by-Pointer
void centimize(double* );

int main()
{ double var = 2.5;
   centimize(&var);
   cout << var << endl;
getch(); return 0; }

void centimize(double* ptrd)
{ *ptrd = *ptrd * 100; }
Pointer Passed to Function
Passing Arrays to Function
const 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++)
Pointer Passed to Function
Assignment # 02
(i)    Write a note on sorting with an example.
(ii)   What is bubble sort? Write a program implementing
       bubble sort using pointers.
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.

More Related Content

PPT
Lec 37 - pointers
PPTX
Learning C++ - Pointers in c++ 2
PPTX
c++ pointers by Amir Hamza Khan (SZABISTIAN)
ODP
Pointers in c++ by minal
PPTX
Pointers in C
PPTX
This pointer
PPT
Lec 36 - pointers
PPTX
Pointers in C/C++ Programming
Lec 37 - pointers
Learning C++ - Pointers in c++ 2
c++ pointers by Amir Hamza Khan (SZABISTIAN)
Pointers in c++ by minal
Pointers in C
This pointer
Lec 36 - pointers
Pointers in C/C++ Programming

What's hot (20)

PPT
C++ Pointers And References
PPT
Pointers in C
PPT
Pointers in C
PPTX
Pointers in C
PPTX
Pointers in c++
PPT
Pointers (Pp Tminimizer)
PPTX
Pointer in C++
PPT
C pointers
PDF
PPTX
C programming - Pointers
PPTX
Presentation on pointer.
PPTX
Pointer in c
PDF
Chapter16 pointer
PDF
Pointers_c
PPTX
COM1407: Working with Pointers
PDF
PPT
pointers
PPTX
Dynamic Memory Allocation in C
PPT
Pointers in c
C++ Pointers And References
Pointers in C
Pointers in C
Pointers in C
Pointers in c++
Pointers (Pp Tminimizer)
Pointer in C++
C pointers
C programming - Pointers
Presentation on pointer.
Pointer in c
Chapter16 pointer
Pointers_c
COM1407: Working with Pointers
pointers
Dynamic Memory Allocation in C
Pointers in c
Ad

Viewers also liked (6)

PPT
Lec 49 - stream-files
PPT
Lec 40.41 - pointers
PPT
Lec 50
PPT
02 c++ Array Pointer
PPT
Lec 42.43 - virtual.functions
PPT
Unit 6 pointers
Lec 49 - stream-files
Lec 40.41 - pointers
Lec 50
02 c++ Array Pointer
Lec 42.43 - virtual.functions
Unit 6 pointers
Ad

Similar to Lec 38.39 - pointers (20)

PPTX
Chp4(ref dynamic)
PDF
C Recursion, Pointers, Dynamic memory management
PDF
Pointers
PPTX
Object Oriented Programming using C++: Ch10 Pointers.pptx
PPTX
C++ Functions | Introduction to programming
PPTX
Pf cs102 programming-9 [pointers]
PPTX
week14Pointers_II. pointers pemrograman dasar C++.pptx
PPT
l7-pointers.ppt
PPTX
Computer Programming for Engineers Spring 2023Lab 8 - Pointers.pptx
PPT
16717 functions in C++
 
PPTX
C++ FUNCTIONS-1.pptx
PPTX
Pointers in c++
PPTX
Learn c++ (functions) with nauman ur rehman
PPTX
Pointers Notes.pptx
PPTX
Array of pointer
PPTX
Passing an Array to a Function (ICT Programming)
PPTX
C Programming : Pointers and Arrays, Pointers and Strings
PPT
Pointer
PPTX
C++ examples &revisions
Chp4(ref dynamic)
C Recursion, Pointers, Dynamic memory management
Pointers
Object Oriented Programming using C++: Ch10 Pointers.pptx
C++ Functions | Introduction to programming
Pf cs102 programming-9 [pointers]
week14Pointers_II. pointers pemrograman dasar C++.pptx
l7-pointers.ppt
Computer Programming for Engineers Spring 2023Lab 8 - Pointers.pptx
16717 functions in C++
 
C++ FUNCTIONS-1.pptx
Pointers in c++
Learn c++ (functions) with nauman ur rehman
Pointers Notes.pptx
Array of pointer
Passing an Array to a Function (ICT Programming)
C Programming : Pointers and Arrays, Pointers and Strings
Pointer
C++ examples &revisions

More from Princess Sam (7)

PPT
Lec 47.48 - stream-files
PPT
Lec 45.46- virtual.functions
PPT
Lec 33 - inheritance
PPT
Lec 30.31 - inheritance
PPT
Lec 28 - operator overloading
PPT
Lec 26.27-operator overloading
PPT
Lec 25 - arrays-strings
Lec 47.48 - stream-files
Lec 45.46- virtual.functions
Lec 33 - inheritance
Lec 30.31 - inheritance
Lec 28 - operator overloading
Lec 26.27-operator overloading
Lec 25 - arrays-strings

Lec 38.39 - pointers

  • 1. Chapter: 10 Pointers Lecture: 38 and 39 Date: 18.10.2012
  • 2. Accessing Addresses int 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. Accessing Contents int 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. Array Accessing Using Index int main() { int intarray[5] = { 31, 54, 77, 52, 93 }; for(int j=0; j<5; j++) cout << intarray[j] << endl; getch(); return 0; }
  • 5. Array Accessing Using Pointers int main() { int intarray[5] = { 31, 54, 77, 52, 93 }; for(int j=0; j<5; j++) cout << *( intarray + j ) << endl; getch(); return 0; }
  • 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. Pass-by-Reference void centimize(double& ); int main() { double var = 2.5; centimize(var); cout << var << endl; getch(); return 0; } void centimize(double& v) { v = v * 100; }
  • 9. Pass-by-Pointer void centimize(double* ); int main() { double var = 2.5; centimize(&var); cout << var << endl; getch(); return 0; } void centimize(double* ptrd) { *ptrd = *ptrd * 100; }
  • 10. Pointer Passed to Function
  • 11. Passing Arrays to Function const 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. Pointer Passed to Function
  • 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. 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.

Editor's Notes

  • #2: Student Book
  • #7: Student Book