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.

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 UsingIndex 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 UsingPointers int main() { int intarray[5] = { 31, 54, 77, 52, 93 }; for(int j=0; j<5; j++) cout << *( intarray + j ) << endl; getch(); return 0; }
  • 6.
  • 7.
    Passing Arguments toFunctions  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& ); intmain() { 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* ); intmain() { double var = 2.5; centimize(&var); cout << var << endl; getch(); return 0; } void centimize(double* ptrd) { *ptrd = *ptrd * 100; }
  • 10.
  • 11.
    Passing Arrays toFunction 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.
  • 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