SlideShare una empresa de Scribd logo
1 de 14
Descargar para leer sin conexión
Pointers in C




 Omar Mukhtar
Outline


    Review of concepts in previous lectures

    Introduction to pointers

    Pointers as function arguments

    Pointers and arrays

    Pointer arithmetic

    Pointer-to-pointer
Review and Background


    Basic data types
       −     Place-holders for numeric data
               
                   Integer numbers (int, short, long)
               
                   Real numbers (float, double)
               
                   Characters / symbols codes (char)

    Arrays
       −     A contiguous list of a particular data type

    Functions
       −     Give “name” to a particular piece of code
       −     Modularization & reuse of code
Pointers


    The most useful and tricky concept in C
    language
       −   Other high level languages abstract-out this
           concept

    The most powerful construct too
       −   Makes C very fast
       −   Direct interaction with hardware
       −   Solves very complicated programming
           problems
What are pointers?


    Just another kind of “placeholder” to hold
    “address” of memory location
        −   Address is also a number
        −   Itself resides on some memory location

       Memory Address      Value
            0x8004           ...
            0x8008                        variable A
                            129
            0x800C           ...
            0x8010         0x8008        address of A
            0x8014           ...
What are pointers?

    Declare variables a, b
        −   int a, b;

    Declare a pointer
        −   int* pa;

    Set value of a
        −   a = 10;

    Point pa to address of a
        −   pa = &a;

    Set value of a using pa
        −   *pa = 12; pa = &b;
Pointer Operators


    “address-of” operator: &
       −   Gets address of a variable

    De-referencing operator: *
       −   Accesses the memory location this pointer
           holds address of
Pointers and Functions


    A function can be passed arguments using
    basic data types
        −    int prod(int a, int b) { return
             a*b; }

    How to return multiple values from function?
        −    void prod_and_sum(int a, int b,
             int*p, int* s)
             { *p = a*b; *s = a+b; }
In & Out Arguments of Function


    A function may like to pass values and get the
    result in the same variables. e.g. a Swap
    function.

    void swap(int* a, int* b) { int c;
     c = *b; *b = *a; *a = c; }

    int a = 5; b = 6;

    swap(&a, &b);

    // a hold 6 and b hold 5 now.
Pointers and Arrays


    Since arrays are a contiguous set of variables
    in memory, we can access them with pointers

    int arr[5];

    int *p = &arr[0];

    *(p+0) = 1;      // arr[0]

    *(p+1) = 2;      // arr[1]

    *(p+2) = 4;      // arr[2]

    *(p+3) = 8;      // arr[3]

    ...
Pointer Arithmetic

    Arithmetic operators work as usual on ordinary
    data types.
        −   int a = 1; a++; // a == 2

    It gets a bit complicated when arithmetic
    operators are used on pointers

    int* p = 0x8004; p++;

    What does p hold now? 0x8005???

    Compiler knows that p is a pointer to integer
    type data, so an increment to it should point to
    next integer in memory. Hence 0x8008.
Pointer Arithmetic

    So an arithmetic operator increases or
    decreases its contents by the size of data type
    it points to

    int* pi = 0x8004; double* pd =
    0x9004; char* pc = 0xa004;

    pi++; // pi == 0x8008

    pd++; // pd == 0x900c

    pc++; // pc == 0xa005

    Only '+' and '-' operator are allowed. '*' and '/'
    are meaningless.
Pointer-to-Pointer

    Pointer variable is just a place-holder of an
    address value, and itself is a variable.
        −   Hence a pointer can hold address of other
            pointer variable. In that case it is called a
            “double pointer”.

    int*p; int **pp; pp = &p;

    e.g a function may like to return a pointer
    value

    void pp_example(int** p) { *p =
    0x8004; }

    int *p; pp_example(&p);
Pointer Pitfalls

    Since pointer holds address of memory
    location, it must never be used without proper
    initialization.

    An uninitialized pointer may hold address of
    some memory location that is protected by
    Operating System. In such case, de-
    referencing a pointer may crash the program.

    An initialized pointer does not know the
    memory location, it is pointing to is, holds a
    valid value or some garbage.

    A pointer cannot track boundaries of an array.

Más contenido relacionado

La actualidad más candente (20)

Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad Salman
 
C programming - Pointers
C programming - PointersC programming - Pointers
C programming - Pointers
 
C Structures and Unions
C Structures and UnionsC Structures and Unions
C Structures and Unions
 
Array Of Pointers
Array Of PointersArray Of Pointers
Array Of Pointers
 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
 
Structure & union
Structure & unionStructure & union
Structure & union
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
 
Arrays and Strings
Arrays and Strings Arrays and Strings
Arrays and Strings
 
Structure in C language
Structure in C languageStructure in C language
Structure in C language
 
Strings in c
Strings in cStrings in c
Strings in c
 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Language
 
Pointers
PointersPointers
Pointers
 
Function C programming
Function C programmingFunction C programming
Function C programming
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
Pointers in c++ by minal
Pointers in c++ by minalPointers in c++ by minal
Pointers in c++ by minal
 
Enums in c
Enums in cEnums in c
Enums in c
 
pointers
pointerspointers
pointers
 
Storage class in c
Storage class in cStorage class in c
Storage class in c
 
C pointer
C pointerC pointer
C pointer
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 

Similar a C Pointers

btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.pptbtech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.pptchintuyadav19
 
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdfEASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdfsudhakargeruganti
 
PPS-POINTERS.pptx
PPS-POINTERS.pptxPPS-POINTERS.pptx
PPS-POINTERS.pptxsajinis3
 
Pointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptxPointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptxRamakrishna Reddy Bijjam
 
Pointers-Computer programming
Pointers-Computer programmingPointers-Computer programming
Pointers-Computer programmingnmahi96
 
pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingpointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingRai University
 
Mca 1 pic u-5 pointer, structure ,union and intro to file handling
Mca 1 pic u-5 pointer, structure ,union and intro to file handlingMca 1 pic u-5 pointer, structure ,union and intro to file handling
Mca 1 pic u-5 pointer, structure ,union and intro to file handlingRai University
 
Btech 1 pic u-5 pointer, structure ,union and intro to file handling
Btech 1 pic u-5 pointer, structure ,union and intro to file handlingBtech 1 pic u-5 pointer, structure ,union and intro to file handling
Btech 1 pic u-5 pointer, structure ,union and intro to file handlingRai University
 
pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingpointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingRai University
 
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handlingBsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handlingRai University
 

Similar a C Pointers (20)

pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
PSPC--UNIT-5.pdf
PSPC--UNIT-5.pdfPSPC--UNIT-5.pdf
PSPC--UNIT-5.pdf
 
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.pptbtech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdfEASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
 
PPS-POINTERS.pptx
PPS-POINTERS.pptxPPS-POINTERS.pptx
PPS-POINTERS.pptx
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
Pointer
PointerPointer
Pointer
 
Pointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptxPointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptx
 
Pointers
PointersPointers
Pointers
 
ch08.ppt
ch08.pptch08.ppt
ch08.ppt
 
Pointers-Computer programming
Pointers-Computer programmingPointers-Computer programming
Pointers-Computer programming
 
COM1407: Working with Pointers
COM1407: Working with PointersCOM1407: Working with Pointers
COM1407: Working with Pointers
 
pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingpointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handling
 
Mca 1 pic u-5 pointer, structure ,union and intro to file handling
Mca 1 pic u-5 pointer, structure ,union and intro to file handlingMca 1 pic u-5 pointer, structure ,union and intro to file handling
Mca 1 pic u-5 pointer, structure ,union and intro to file handling
 
Advanced pointers
Advanced pointersAdvanced pointers
Advanced pointers
 
Lecture 18 - Pointers
Lecture 18 - PointersLecture 18 - Pointers
Lecture 18 - Pointers
 
Btech 1 pic u-5 pointer, structure ,union and intro to file handling
Btech 1 pic u-5 pointer, structure ,union and intro to file handlingBtech 1 pic u-5 pointer, structure ,union and intro to file handling
Btech 1 pic u-5 pointer, structure ,union and intro to file handling
 
pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingpointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handling
 
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handlingBsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
 

Último

Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024The Digital Insurer
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 

Último (20)

Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 

C Pointers

  • 1. Pointers in C Omar Mukhtar
  • 2. Outline  Review of concepts in previous lectures  Introduction to pointers  Pointers as function arguments  Pointers and arrays  Pointer arithmetic  Pointer-to-pointer
  • 3. Review and Background  Basic data types − Place-holders for numeric data  Integer numbers (int, short, long)  Real numbers (float, double)  Characters / symbols codes (char)  Arrays − A contiguous list of a particular data type  Functions − Give “name” to a particular piece of code − Modularization & reuse of code
  • 4. Pointers  The most useful and tricky concept in C language − Other high level languages abstract-out this concept  The most powerful construct too − Makes C very fast − Direct interaction with hardware − Solves very complicated programming problems
  • 5. What are pointers?  Just another kind of “placeholder” to hold “address” of memory location − Address is also a number − Itself resides on some memory location Memory Address Value 0x8004 ... 0x8008 variable A 129 0x800C ... 0x8010 0x8008 address of A 0x8014 ...
  • 6. What are pointers?  Declare variables a, b − int a, b;  Declare a pointer − int* pa;  Set value of a − a = 10;  Point pa to address of a − pa = &a;  Set value of a using pa − *pa = 12; pa = &b;
  • 7. Pointer Operators  “address-of” operator: & − Gets address of a variable  De-referencing operator: * − Accesses the memory location this pointer holds address of
  • 8. Pointers and Functions  A function can be passed arguments using basic data types − int prod(int a, int b) { return a*b; }  How to return multiple values from function? − void prod_and_sum(int a, int b, int*p, int* s) { *p = a*b; *s = a+b; }
  • 9. In & Out Arguments of Function  A function may like to pass values and get the result in the same variables. e.g. a Swap function.  void swap(int* a, int* b) { int c; c = *b; *b = *a; *a = c; }  int a = 5; b = 6;  swap(&a, &b);  // a hold 6 and b hold 5 now.
  • 10. Pointers and Arrays  Since arrays are a contiguous set of variables in memory, we can access them with pointers  int arr[5];  int *p = &arr[0];  *(p+0) = 1; // arr[0]  *(p+1) = 2; // arr[1]  *(p+2) = 4; // arr[2]  *(p+3) = 8; // arr[3]  ...
  • 11. Pointer Arithmetic  Arithmetic operators work as usual on ordinary data types. − int a = 1; a++; // a == 2  It gets a bit complicated when arithmetic operators are used on pointers  int* p = 0x8004; p++;  What does p hold now? 0x8005???  Compiler knows that p is a pointer to integer type data, so an increment to it should point to next integer in memory. Hence 0x8008.
  • 12. Pointer Arithmetic  So an arithmetic operator increases or decreases its contents by the size of data type it points to  int* pi = 0x8004; double* pd = 0x9004; char* pc = 0xa004;  pi++; // pi == 0x8008  pd++; // pd == 0x900c  pc++; // pc == 0xa005  Only '+' and '-' operator are allowed. '*' and '/' are meaningless.
  • 13. Pointer-to-Pointer  Pointer variable is just a place-holder of an address value, and itself is a variable. − Hence a pointer can hold address of other pointer variable. In that case it is called a “double pointer”.  int*p; int **pp; pp = &p;  e.g a function may like to return a pointer value  void pp_example(int** p) { *p = 0x8004; }  int *p; pp_example(&p);
  • 14. Pointer Pitfalls  Since pointer holds address of memory location, it must never be used without proper initialization.  An uninitialized pointer may hold address of some memory location that is protected by Operating System. In such case, de- referencing a pointer may crash the program.  An initialized pointer does not know the memory location, it is pointing to is, holds a valid value or some garbage.  A pointer cannot track boundaries of an array.