lecture 12 pointer

Upload: arun-mohanaraj

Post on 04-Apr-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 Lecture 12 Pointer

    1/39

    1

    BITG 1113:Pointer

    LECTURE 12

  • 7/30/2019 Lecture 12 Pointer

    2/39

    2

    Objectives:

    To understand the concept of pointer.

    Declaration and Assignment of a Pointer.

    Arithmetic Operation using Pointer.

    Pointer to pointer.

    Pointers and functions.

    Relationship between a Pointer and anArray.

    Pointer to structure.

  • 7/30/2019 Lecture 12 Pointer

    3/39

    3

    Introduction

    A pointer is a derived type. Its value may be

    any of the addresses available in the computer

    for storing and accessing data.

    In a computer, every process is stored in a

    compartment or small area inside the memory.

    These memories have their own addresses.

    By using this address, the computer canidentify where a data or an instruction is stored

    physically in a computer.

  • 7/30/2019 Lecture 12 Pointer

    4/39

    4

    Introduction

    Variable declaration :

    int nilai;

    Only shows the value that is stored inside

    nilai, without knowing where it is stored.

    The name of the variable is an @ for theaddress where it is stored.

    Each time, the variable being used, the

    computer will translate the name of that

    variable to its address.

    By using pointer, the programmer can

    determine the address for any location

    where a value is stored.

  • 7/30/2019 Lecture 12 Pointer

    5/39

    5

    Pointer Declaration and Definition

    Syntax :

    type * identifier;

    Example :

    1.char a=A;

    char *p; OR

    p = &a; char *p = &a;

    2. double b=2.5;

    double *q; OR

    q = &b; double *q=&b;

    The type of pointer indicates

    the type of value it refers.

  • 7/30/2019 Lecture 12 Pointer

    6/39

    6

    int x=7;int *p;

    p = &x;

    Suppose that the address of the int variable x is 9640

    x 7 9640p

    9640

    the variable p

    points to x

    9642

    Pointer Declaration and definition

  • 7/30/2019 Lecture 12 Pointer

    7/39

    #include

    void main()

    {

    int pertama=8;

    int *pAlamat;

    pAlamat = &pertama;

    cout

  • 7/30/2019 Lecture 12 Pointer

    8/39

    #include

    int main()

    {

    int pertama=7;

    int *pAlamat;

    pAlamat = &pertama;

    cout

  • 7/30/2019 Lecture 12 Pointer

    9/39

    9

    The following program slice swaps thecontents of the variables char1 and char2

    but uses the address and dereferencing

    operators to do so.

    Line #includemain()

    {

    1 char char1 = A;

    2 char char2 = Z;

    3 char temp;

    4 char* charPtr;

    5 charPtr = &char1;

    6 temp = *charPtr;

    7 *charPtr = char2;

    8 char2 = temp;

    }

    Example

  • 7/30/2019 Lecture 12 Pointer

    10/39

    10

    A Z

    char1 char2 temp

    1293 7757 2131

    line

    1 char char1 = A;2 char char2 = Z;

    3 char temp;

  • 7/30/2019 Lecture 12 Pointer

    11/39

    11

    A Z

    char1 char2 temp

    1293 7757 2131

    line

    4 char* charPtr;

    charPtr

    4455

  • 7/30/2019 Lecture 12 Pointer

    12/39

  • 7/30/2019 Lecture 12 Pointer

    13/39

    13

    A Z A

    char1 char2 temp

    7757 2131

    line

    6 temp = *charPtr;

    1293

    charPtr

    44551293

  • 7/30/2019 Lecture 12 Pointer

    14/39

    14

    Z Z A

    1293 7757 2131

    line

    7 *charPtr=char2;

    1293

    4455

    char1 char2 temp charPtr

  • 7/30/2019 Lecture 12 Pointer

    15/39

    15

    Z A A

    char1 char2 temp

    1293 7757 2131

    line

    8 char2 = temp;

    1293

    charPtr

    4455

  • 7/30/2019 Lecture 12 Pointer

    16/39

  • 7/30/2019 Lecture 12 Pointer

    17/39

  • 7/30/2019 Lecture 12 Pointer

    18/39

    18

    O H 941

    c d p1

    6940 2131

    6940

    p2

    4455

    line

    5 temp = p1;

    941

    temp

    941

  • 7/30/2019 Lecture 12 Pointer

    19/39

    19

    O H 6940

    c d p1

    6940 2131

    6940

    p2

    4455

    line

    6 p1 = p2;

    941

    temp

    941

  • 7/30/2019 Lecture 12 Pointer

    20/39

    O H 6940

    c d p1

    6940 2131

    941

    p2

    4455

    line

    7 p2 = temp;

    941

    temp

    941

    line

    8 cout

  • 7/30/2019 Lecture 12 Pointer

    21/39

    21

    Arithmetic Operation Using Pointer

    Arithmetic operation can also be done using

    pointer.

    A pointer will take the value stored at the

    address that is kept by the pointer andcompute them just like operations on

    normal variables such as add, multiply,

    divide, minus and unary operator.

  • 7/30/2019 Lecture 12 Pointer

    22/39

    22

    //Example:

    #include

    void main()

    {

    int nombor = 6, jumlah;

    int *pnum=&nombor;

    jumlah = *pnum * *pnum;

    cout

  • 7/30/2019 Lecture 12 Pointer

    23/39

    23

    Pointer to pointerSo far the pointers we have been using have

    pointed directly to data. It is possible and

    often with advanced data structures necessary

    to use pointers that point to other pointers.

    eg: *p dereferenced once

    **q dereferenced twice

  • 7/30/2019 Lecture 12 Pointer

    24/39

    24

    #includemain() {

    int a;

    int *p;

    int **q;

    a = 58;

    p = &a;

    q = &p;

    cout

  • 7/30/2019 Lecture 12 Pointer

    25/39

    25

    Pointers and functions

    C++ provides two ways to pass parameters tofunctions : pass by value and pass by reference.

    When we passed by reference, C++ passes the

    address of the parameter variable, and theparameter name becames an alias for the variable.

    We can add an alternative to pass by reference:

    pass a pointer and use it to change the originalvariable.

  • 7/30/2019 Lecture 12 Pointer

    26/39

    26

    void main()

    {//Pass by reference

    int a = 5;

    int b = 7;

    exchange (a,b);}

    void exchange(int& x, int& y)

    {

    int temp = x;x=y;

    y= temp;

    return;

    }//exchange

    Example

    7 5

    a b

    x y

    5

    temp

  • 7/30/2019 Lecture 12 Pointer

    27/39

    27

    Examplevoid main()

    {//Passing pointers

    int a = 5;

    int b = 7;

    exchange (&a,&b);}

    void exchange(int* px, int* py)

    {

    int temp = *px;*px=*py;

    *py= temp;

    return;

    }//exchange

    7 5

    a b

    px

    y

    5

    temp

    py

  • 7/30/2019 Lecture 12 Pointer

    28/39

    28

    Relationship between aPointer and an Array

    A pointer and an array have a close

    relationship.

    In C++ Programming Language, the name ofan array without the index is the first address

    of that array. In other words, the name of an

    array is actually a pointer to that array.

    So, the name of an array can be used to beset as an initial value for a pointer.

    The name of an array will refer to the

    address of the first element of that array.

  • 7/30/2019 Lecture 12 Pointer

    29/39

    29

    The difference between a pointer and an arrays

    name is, a pointer points to any address in the

    memory but the name of an array points only to

    the first element of that array and it is fixed.

    Example :

    double value[10];double *pValue = value;

    From the declaration above, *pValue will store

    the first element of array value which is theaddress of value[0].

    Relationship between aPointer and an Array

  • 7/30/2019 Lecture 12 Pointer

    30/39

    30

    To achieve the next element in an array, use thisformula :-

    *( penuding + i )

    where , i represents the location of the element

    in an array.

    Example :

    double value[5]={2.5,4.1,5.6,4.7,4.3};

    double *pValue = value;

    cout

  • 7/30/2019 Lecture 12 Pointer

    31/39

    Example of program that use anarray and index

    #include main()

    {

    const int saiz = 5;

    int i;

    int gred[saiz]={98,88,90,65,83};

    for( i = 0; i

  • 7/30/2019 Lecture 12 Pointer

    32/39

    32

    From the example, each element in arraygred can be reach using pointer :

    Array Pointer Value in Array

    gred[0]

    gred[1]

    gred[2]

    gred[3]

    gred[4]

    *pNilai

    *( pNilai + 1 )

    *( pNilai + 2 )

    *( pNilai + 3 )

    *( pNilai + 4 )

    98

    88

    90

    65

    83

    Relationship between aPointer and an Array

  • 7/30/2019 Lecture 12 Pointer

    33/39

    33

    Relationship between aPointer and an Array

    Example :

    double value[5]={2.5,4.1,5.6,4.7,4.3};

    double *pValue = &value[1];

    cout

  • 7/30/2019 Lecture 12 Pointer

    34/39

    34

    Structure can be accessed through pointers.

    Example : use SAMPLE structure with pointers.

    The first thing is define a pointer for the

    structure as shown below.

    Refer to whole structure

    Pointer to Structure

  • 7/30/2019 Lecture 12 Pointer

    35/39

    35

    The parentheses are absolutely necessary

    because the precedence priority of the

    member operator is higher than the priority of

    the indirection operator.

    If the parentheses are not used, C++ applies

    the dot operator first and the asterisk operator

    next.

    In other words, *ptr.x is interpreted as

    *(ptr.x)

    Pointer to Structure

  • 7/30/2019 Lecture 12 Pointer

    36/39

    36

    Pointer to Structure

  • 7/30/2019 Lecture 12 Pointer

    37/39

    37

    Another operator that eliminates the problem

    with pointers to structures the selectionoperator.

    The selection operator is at the same level in the

    Precedence Table as the member operator.

    The token for the selection operator is an arrow

    formed by the minus sign and the greater than

    symbol (->).

    The token is placed immediately after the pointer

    identifiers and before the member to be

    referenced.

    Selection Operator

  • 7/30/2019 Lecture 12 Pointer

    38/39

    38

    (* pointerName) . fieldName

    SAME AS

    pointerName -> fieldName

    Selection Operator

  • 7/30/2019 Lecture 12 Pointer

    39/39

    39

    Selection Operator