SlideShare uma empresa Scribd logo
1 de 29
Chapter: 10


              Pointers
                Lecture: 37
              Date: 16.10.2012
What are Pointers Used For?

   Accessing array elements
   Passing arguments to a function when the function needs
    to modify the original argument
   Passing arrays and strings to functions
   Obtaining memory from the system
   Creating data structures such as linked lists
Memory and Addresses



    1270

    1271

    1272

    1273

    1274

    1275


   Computer Memory
Memory and Addresses
   Addresses   Locations


     1270

     1271

     1272

     1273

     1274

     1275


   Computer Memory
Memory and Addresses




int IntVar1;    //2 bytes

int IntVar2;   //2 byte
Memory and Addresses
                            Addresses   Locations


                              1270

                              1271
int IntVar1;    //2 bytes
                              1272                  IntVar1
int IntVar2;   //2 byte       1273

                              1274

                              1275                  IntVar2

                            Computer Memory
Memory and Addresses




int IntVar1 = 25;

int IntVar2 = 11;
Memory and Addresses
                     Addresses     Locations


                       1270

                       1271
int IntVar1 = 25;                 25           IntVar1
                       1272

int IntVar2 = 11;      1273

                       1274
                                  11           IntVar2
                       1275




                              Contents/Data
Memory and Addresses
   In some cases we may be interested in knowing the address
    where our variable is being stored during runtime.
   The address that locates a variable within memory is what
    we call a reference to that variable.
    e.g.,
                              & IntVar;

                       Address-of/reference

   When preceding the name of the variable “IntVar” with the
                           operator
    reference operator (&) we are no longer talking about the
    content of the variable itself, but about its reference (i.e., its
    address in memory).
Memory and Addresses
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int IntVar1;
int IntVar2;
cout << &IntVar1 << endl     //print the addresses
      << &IntVar2 << endl;
getch();
return 0; }
Pointer Variable
    The variable that stores the reference to another variable is
     what we call a pointer.
    e.g.,




               ptr = &InVar;
Pointer Variable
    The variable that stores the reference to another variable is
     what we call a pointer.
    e.g.,
               Pointer-to             Pointer/Pointer-variable


               int * ptr; //variable “ptr” as a pointer-to “int”
                     ptr = &InVar;
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; }
int* ptr;                                1270
ptr = &IntVar1;           ptr
                                         1271
  cout << ptr ;
                          1271
                                                25   IntVar1
                                         1272
                  ptr points-to to the
                                         1273
                  address of IntVar1
                                         1274
                                                11
                                         1275        IntVar2

                                         1270

                                         1271
                                                25
                                         1272        IntVar1
int* ptr;                ptr             1273
ptr = &IntVar2;
                          1274           1274
  cout << ptr ;
                                                11
                  ptr points-to to the   1275        IntVar2
                  address of IntVar2
Accessing Contens
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; }
ptr
int* ptr;
ptr = &IntVar1;                       25   IntVar1
                         *ptr is 25
  cout << *ptr ;

                                      11
 deference /indirection operator.
                                           IntVar2
 Expression *ptr means the value of
 the variable pointed to by ptr.



                                      25
                                           IntVar1
                              ptr
int* ptr;
ptr = &IntVar2;                       11
                         *ptr is 11
                                           IntVar2
  cout << *ptr ;
Pointer to Void
   The address that is put in a pointer variable must be the
    same type as the pointer, for example, the address of a float
    variable can’t be assigned to a pointer to int.

    float floVar = 25.67;
    int* ptrInt = &floVar;
Pointer to Void
   The address that is put in a pointer variable must be the
    same type as the pointer, for example, the address of a float
    variable can’t be assigned to a pointer to int.

    float floVar = 25.67;
    int* ptrInt = &floVar; //ERROR: can’t assign float* to int*
Pointer to Void
   The address that is put in a pointer variable must be the
    same type as the pointer, for example, the address of a float
    variable can’t be assigned to a pointer to int.
    float floVar = 25.67;
    int* ptrInt;
    ptrInt = &floVar; //ERROR: can’t assign float* to int*

   Exception to that case is a general-purpose pointer that can
    point to any data type, e.g.,

    void* ptrVoid; //pointer to void
Pointer to Void
   The address that is put in a pointer variable must be the
    same type as the pointer, for example, the address of a float
    variable can’t be assigned to a pointer to int.
    float floVar = 25.67;
    int* ptrInt;
    ptrInt = &floVar; //ERROR: can’t assign float* to int*

   Exception to that case is a general-purpose pointer that can
    point to any data type, e.g.,
    void* ptrVoid; //pointer to void
        ptrVoid = &floVar; //OK
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*); //prototype
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
Linked List
   Linked list is another way to store data besides storing
    data in arrays.
   However, arrays suffer from the necessity to declare a
    fixed-size array before running the program.

Mais conteúdo relacionado

Mais procurados (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
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Pointers
PointersPointers
Pointers
 
Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)
 
C pointers
C pointersC pointers
C pointers
 
C programming - Pointers
C programming - PointersC programming - Pointers
C programming - Pointers
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
Presentation on pointer.
Presentation on pointer.Presentation on pointer.
Presentation on pointer.
 
Pointers_c
Pointers_cPointers_c
Pointers_c
 
COM1407: Working with Pointers
COM1407: Working with PointersCOM1407: Working with Pointers
COM1407: Working with Pointers
 
C pointer basics
C pointer basicsC pointer basics
C pointer basics
 
Pointer
PointerPointer
Pointer
 
Pointer in c++ part2
Pointer in c++ part2Pointer in c++ part2
Pointer in c++ part2
 
pointers
pointerspointers
pointers
 
Chapter16 pointer
Chapter16 pointerChapter16 pointer
Chapter16 pointer
 
Dynamic Memory Allocation in C
Dynamic Memory Allocation in CDynamic Memory Allocation in C
Dynamic Memory Allocation in C
 

Destaque

Advanced CPP Lecture 2- Summer School 2014 - ACA CSE IITK
Advanced CPP Lecture 2- Summer School 2014 - ACA CSE IITKAdvanced CPP Lecture 2- Summer School 2014 - ACA CSE IITK
Advanced CPP Lecture 2- Summer School 2014 - ACA CSE IITKPankaj Prateek
 
Diploma ii cfpc- u-5.3 pointer, structure ,union and intro to file handling
Diploma ii  cfpc- u-5.3 pointer, structure ,union and intro to file handlingDiploma ii  cfpc- u-5.3 pointer, structure ,union and intro to file handling
Diploma ii cfpc- u-5.3 pointer, structure ,union and intro to file handlingRai University
 
Lec 49 - stream-files
Lec 49 - stream-filesLec 49 - stream-files
Lec 49 - stream-filesPrincess Sam
 
Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK
Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITKAdvanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK
Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITKPankaj Prateek
 
Lec 30.31 - inheritance
Lec 30.31 -  inheritanceLec 30.31 -  inheritance
Lec 30.31 - inheritancePrincess Sam
 
Lec 42.43 - virtual.functions
Lec 42.43 - virtual.functionsLec 42.43 - virtual.functions
Lec 42.43 - virtual.functionsPrincess Sam
 

Destaque (7)

Advanced CPP Lecture 2- Summer School 2014 - ACA CSE IITK
Advanced CPP Lecture 2- Summer School 2014 - ACA CSE IITKAdvanced CPP Lecture 2- Summer School 2014 - ACA CSE IITK
Advanced CPP Lecture 2- Summer School 2014 - ACA CSE IITK
 
Diploma ii cfpc- u-5.3 pointer, structure ,union and intro to file handling
Diploma ii  cfpc- u-5.3 pointer, structure ,union and intro to file handlingDiploma ii  cfpc- u-5.3 pointer, structure ,union and intro to file handling
Diploma ii cfpc- u-5.3 pointer, structure ,union and intro to file handling
 
Lec 49 - stream-files
Lec 49 - stream-filesLec 49 - stream-files
Lec 49 - stream-files
 
Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK
Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITKAdvanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK
Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK
 
Preso#5
Preso#5Preso#5
Preso#5
 
Lec 30.31 - inheritance
Lec 30.31 -  inheritanceLec 30.31 -  inheritance
Lec 30.31 - inheritance
 
Lec 42.43 - virtual.functions
Lec 42.43 - virtual.functionsLec 42.43 - virtual.functions
Lec 42.43 - virtual.functions
 

Semelhante a Pointers Guide: Uses, Memory, References and Functions

Lec 40.41 - pointers
Lec 40.41 -  pointersLec 40.41 -  pointers
Lec 40.41 - pointersPrincess Sam
 
Chapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdfChapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdfTamiratDejene1
 
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdfPOINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdfSowmyaJyothi3
 
Pointer Basics,Constant Pointers & Pointer to Constant.
Pointer Basics,Constant Pointers & Pointer to Constant.Pointer Basics,Constant Pointers & Pointer to Constant.
Pointer Basics,Constant Pointers & Pointer to Constant.Meghaj Mallick
 
FP 201 - Unit 6
FP 201 - Unit 6FP 201 - Unit 6
FP 201 - Unit 6rohassanie
 
Unit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxUnit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxajajkhan16
 
detailed information about Pointers in c language
detailed information about Pointers in c languagedetailed information about Pointers in c language
detailed information about Pointers in c languagegourav kottawar
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 

Semelhante a Pointers Guide: Uses, Memory, References and Functions (20)

Lec 40.41 - pointers
Lec 40.41 -  pointersLec 40.41 -  pointers
Lec 40.41 - pointers
 
Chapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdfChapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdf
 
Pointers in c
Pointers in cPointers in c
Pointers in c
 
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdfPOINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
 
Pointers
PointersPointers
Pointers
 
Pointer Basics,Constant Pointers & Pointer to Constant.
Pointer Basics,Constant Pointers & Pointer to Constant.Pointer Basics,Constant Pointers & Pointer to Constant.
Pointer Basics,Constant Pointers & Pointer to Constant.
 
PSPC--UNIT-5.pdf
PSPC--UNIT-5.pdfPSPC--UNIT-5.pdf
PSPC--UNIT-5.pdf
 
Pointer
PointerPointer
Pointer
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
FP 201 - Unit 6
FP 201 - Unit 6FP 201 - Unit 6
FP 201 - Unit 6
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Pointer
PointerPointer
Pointer
 
POINTERS IN C
POINTERS IN CPOINTERS IN C
POINTERS IN C
 
Unit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxUnit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptx
 
detailed information about Pointers in c language
detailed information about Pointers in c languagedetailed information about Pointers in c language
detailed information about Pointers in c language
 
oop Lecture 17
oop Lecture 17oop Lecture 17
oop Lecture 17
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Link list
Link listLink list
Link list
 

Mais 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 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
 

Mais de Princess Sam (7)

Lec 50
Lec 50Lec 50
Lec 50
 
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 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
 

Pointers Guide: Uses, Memory, References and Functions

  • 1. Chapter: 10 Pointers Lecture: 37 Date: 16.10.2012
  • 2. What are Pointers Used For?  Accessing array elements  Passing arguments to a function when the function needs to modify the original argument  Passing arrays and strings to functions  Obtaining memory from the system  Creating data structures such as linked lists
  • 3. Memory and Addresses 1270 1271 1272 1273 1274 1275 Computer Memory
  • 4. Memory and Addresses Addresses Locations 1270 1271 1272 1273 1274 1275 Computer Memory
  • 5. Memory and Addresses int IntVar1; //2 bytes int IntVar2; //2 byte
  • 6. Memory and Addresses Addresses Locations 1270 1271 int IntVar1; //2 bytes 1272 IntVar1 int IntVar2; //2 byte 1273 1274 1275 IntVar2 Computer Memory
  • 7. Memory and Addresses int IntVar1 = 25; int IntVar2 = 11;
  • 8. Memory and Addresses Addresses Locations 1270 1271 int IntVar1 = 25; 25 IntVar1 1272 int IntVar2 = 11; 1273 1274 11 IntVar2 1275 Contents/Data
  • 9.
  • 10. Memory and Addresses  In some cases we may be interested in knowing the address where our variable is being stored during runtime.  The address that locates a variable within memory is what we call a reference to that variable. e.g., & IntVar; Address-of/reference  When preceding the name of the variable “IntVar” with the operator reference operator (&) we are no longer talking about the content of the variable itself, but about its reference (i.e., its address in memory).
  • 11. Memory and Addresses #include <iostream> #include <conio.h> using namespace std; int main() { int IntVar1; int IntVar2; cout << &IntVar1 << endl //print the addresses << &IntVar2 << endl; getch(); return 0; }
  • 12. Pointer Variable  The variable that stores the reference to another variable is what we call a pointer. e.g., ptr = &InVar;
  • 13. Pointer Variable  The variable that stores the reference to another variable is what we call a pointer. e.g., Pointer-to Pointer/Pointer-variable int * ptr; //variable “ptr” as a pointer-to “int” ptr = &InVar;
  • 14. 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; }
  • 15. int* ptr; 1270 ptr = &IntVar1; ptr 1271 cout << ptr ; 1271 25 IntVar1 1272 ptr points-to to the 1273 address of IntVar1 1274 11 1275 IntVar2 1270 1271 25 1272 IntVar1 int* ptr; ptr 1273 ptr = &IntVar2; 1274 1274 cout << ptr ; 11 ptr points-to to the 1275 IntVar2 address of IntVar2
  • 16. Accessing Contens 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; }
  • 17. ptr int* ptr; ptr = &IntVar1; 25 IntVar1 *ptr is 25 cout << *ptr ; 11 deference /indirection operator. IntVar2 Expression *ptr means the value of the variable pointed to by ptr. 25 IntVar1 ptr int* ptr; ptr = &IntVar2; 11 *ptr is 11 IntVar2 cout << *ptr ;
  • 18. Pointer to Void  The address that is put in a pointer variable must be the same type as the pointer, for example, the address of a float variable can’t be assigned to a pointer to int. float floVar = 25.67; int* ptrInt = &floVar;
  • 19. Pointer to Void  The address that is put in a pointer variable must be the same type as the pointer, for example, the address of a float variable can’t be assigned to a pointer to int. float floVar = 25.67; int* ptrInt = &floVar; //ERROR: can’t assign float* to int*
  • 20. Pointer to Void  The address that is put in a pointer variable must be the same type as the pointer, for example, the address of a float variable can’t be assigned to a pointer to int. float floVar = 25.67; int* ptrInt; ptrInt = &floVar; //ERROR: can’t assign float* to int*  Exception to that case is a general-purpose pointer that can point to any data type, e.g., void* ptrVoid; //pointer to void
  • 21. Pointer to Void  The address that is put in a pointer variable must be the same type as the pointer, for example, the address of a float variable can’t be assigned to a pointer to int. float floVar = 25.67; int* ptrInt; ptrInt = &floVar; //ERROR: can’t assign float* to int*  Exception to that case is a general-purpose pointer that can point to any data type, e.g., void* ptrVoid; //pointer to void ptrVoid = &floVar; //OK
  • 23. 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.
  • 24. 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; }
  • 25. 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; }
  • 26. Pointer Passed to Function
  • 27. Passing Arrays to Function const int MAX = 5; void centimize(double*); //prototype 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++)
  • 28. Pointer Passed to Function
  • 29. Linked List  Linked list is another way to store data besides storing data in arrays.  However, arrays suffer from the necessity to declare a fixed-size array before running the program.

Notas do Editor

  1. Student Book
  2. Student Book
  3. Student Book