SlideShare une entreprise Scribd logo
1  sur  14
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.

Contenu connexe

Tendances (20)

C++ Pointers And References
C++ Pointers And ReferencesC++ Pointers And References
C++ Pointers And References
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
 
C pointers
C pointersC pointers
C pointers
 
Pointers
PointersPointers
Pointers
 
C programming - Pointers
C programming - PointersC programming - Pointers
C programming - Pointers
 
Presentation on pointer.
Presentation on pointer.Presentation on pointer.
Presentation on pointer.
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
Chapter16 pointer
Chapter16 pointerChapter16 pointer
Chapter16 pointer
 
Pointers_c
Pointers_cPointers_c
Pointers_c
 
C pointer basics
C pointer basicsC pointer basics
C pointer basics
 
COM1407: Working with Pointers
COM1407: Working with PointersCOM1407: Working with Pointers
COM1407: Working with Pointers
 
Pointer
PointerPointer
Pointer
 
pointers
pointerspointers
pointers
 
Dynamic Memory Allocation in C
Dynamic Memory Allocation in CDynamic Memory Allocation in C
Dynamic Memory Allocation in C
 
Pointers in c
Pointers in cPointers in c
Pointers in c
 

En vedette

En vedette (6)

Lec 49 - stream-files
Lec 49 - stream-filesLec 49 - stream-files
Lec 49 - stream-files
 
Lec 40.41 - pointers
Lec 40.41 -  pointersLec 40.41 -  pointers
Lec 40.41 - pointers
 
Lec 50
Lec 50Lec 50
Lec 50
 
02 c++ Array Pointer
02 c++ Array Pointer02 c++ Array Pointer
02 c++ Array Pointer
 
Lec 42.43 - virtual.functions
Lec 42.43 - virtual.functionsLec 42.43 - virtual.functions
Lec 42.43 - virtual.functions
 
Unit 6 pointers
Unit 6   pointersUnit 6   pointers
Unit 6 pointers
 

Similaire à Lec 38.39 - pointers

how to reuse code
how to reuse codehow to reuse code
how to reuse codejleed1
 
Learn c++ (functions) with nauman ur rehman
Learn  c++ (functions) with nauman ur rehmanLearn  c++ (functions) with nauman ur rehman
Learn c++ (functions) with nauman ur rehmanNauman Rehman
 
Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functionsTAlha MAlik
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxShashiShash2
 
FP 201 - Unit 6
FP 201 - Unit 6FP 201 - Unit 6
FP 201 - Unit 6rohassanie
 
Object Oriented Technologies
Object Oriented TechnologiesObject Oriented Technologies
Object Oriented TechnologiesUmesh Nikam
 
C++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfC++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfyamew16788
 
Pointers in c++ programming presentation
Pointers in c++ programming presentationPointers in c++ programming presentation
Pointers in c++ programming presentationSourabhGour9
 
C++ Programming - 2nd Study
C++ Programming - 2nd StudyC++ Programming - 2nd Study
C++ Programming - 2nd StudyChris Ohk
 
Advanced C programming
Advanced C programmingAdvanced C programming
Advanced C programmingClaus Wu
 
Modify this code to use multiple threads with the same data1.Modif.pdf
Modify this code to use multiple threads with the same data1.Modif.pdfModify this code to use multiple threads with the same data1.Modif.pdf
Modify this code to use multiple threads with the same data1.Modif.pdfmallik3000
 
Lecture 5Arrays on c++ for Beginner.pptx
Lecture 5Arrays on c++ for Beginner.pptxLecture 5Arrays on c++ for Beginner.pptx
Lecture 5Arrays on c++ for Beginner.pptxarjurakibulhasanrrr7
 
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String ProcessingDynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String ProcessingMeghaj Mallick
 

Similaire à Lec 38.39 - pointers (20)

how to reuse code
how to reuse codehow to reuse code
how to reuse code
 
Learn c++ (functions) with nauman ur rehman
Learn  c++ (functions) with nauman ur rehmanLearn  c++ (functions) with nauman ur rehman
Learn c++ (functions) with nauman ur rehman
 
Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functions
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptx
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptx
 
FP 201 - Unit 6
FP 201 - Unit 6FP 201 - Unit 6
FP 201 - Unit 6
 
Lecture2.ppt
Lecture2.pptLecture2.ppt
Lecture2.ppt
 
Object Oriented Technologies
Object Oriented TechnologiesObject Oriented Technologies
Object Oriented Technologies
 
Pointer.pptx
Pointer.pptxPointer.pptx
Pointer.pptx
 
C++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfC++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdf
 
Computer Programming- Lecture 7
Computer Programming- Lecture 7Computer Programming- Lecture 7
Computer Programming- Lecture 7
 
Pointers in c++ programming presentation
Pointers in c++ programming presentationPointers in c++ programming presentation
Pointers in c++ programming presentation
 
pointers
pointerspointers
pointers
 
pointers
pointerspointers
pointers
 
C++ L05-Functions
C++ L05-FunctionsC++ L05-Functions
C++ L05-Functions
 
C++ Programming - 2nd Study
C++ Programming - 2nd StudyC++ Programming - 2nd Study
C++ Programming - 2nd Study
 
Advanced C programming
Advanced C programmingAdvanced C programming
Advanced C programming
 
Modify this code to use multiple threads with the same data1.Modif.pdf
Modify this code to use multiple threads with the same data1.Modif.pdfModify this code to use multiple threads with the same data1.Modif.pdf
Modify this code to use multiple threads with the same data1.Modif.pdf
 
Lecture 5Arrays on c++ for Beginner.pptx
Lecture 5Arrays on c++ for Beginner.pptxLecture 5Arrays on c++ for Beginner.pptx
Lecture 5Arrays on c++ for Beginner.pptx
 
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String ProcessingDynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
 

Plus de Princess Sam

Lec 47.48 - stream-files
Lec 47.48 - stream-filesLec 47.48 - stream-files
Lec 47.48 - stream-filesPrincess Sam
 
Lec 45.46- virtual.functions
Lec 45.46- virtual.functionsLec 45.46- virtual.functions
Lec 45.46- virtual.functionsPrincess Sam
 
Lec 33 - inheritance
Lec 33 -  inheritanceLec 33 -  inheritance
Lec 33 - inheritancePrincess Sam
 
Lec 30.31 - inheritance
Lec 30.31 -  inheritanceLec 30.31 -  inheritance
Lec 30.31 - inheritancePrincess Sam
 
Lec 28 - operator overloading
Lec 28 - operator overloadingLec 28 - operator overloading
Lec 28 - operator overloadingPrincess Sam
 
Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloadingPrincess Sam
 
Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-stringsPrincess Sam
 

Plus de Princess Sam (7)

Lec 47.48 - stream-files
Lec 47.48 - stream-filesLec 47.48 - stream-files
Lec 47.48 - stream-files
 
Lec 45.46- virtual.functions
Lec 45.46- virtual.functionsLec 45.46- virtual.functions
Lec 45.46- virtual.functions
 
Lec 33 - inheritance
Lec 33 -  inheritanceLec 33 -  inheritance
Lec 33 - inheritance
 
Lec 30.31 - inheritance
Lec 30.31 -  inheritanceLec 30.31 -  inheritance
Lec 30.31 - inheritance
 
Lec 28 - operator overloading
Lec 28 - operator overloadingLec 28 - operator overloading
Lec 28 - operator overloading
 
Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloading
 
Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
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.

Notes de l'éditeur

  1. Student Book
  2. Student Book