02 Pointers

4
 #02 Pointers 242-207 FUNDAMENTAL PROGRAMMING I DEPARTMENT OF COMPUTER ENGINEERING, PSU 1 v1.1 Contents Introduction to Pointer  Call by Reference Pointer & Array Pointer & Structure 2 Pointers To code Call by Reference To build lists, queues, stacks, trees, e tc. Overview: Recap: Variable is just a box in memory. 3 , *1, *2 // 1 & // 2 1 // *1 // ("%", * 2) //  x ptr1 ptr2 Declaring a Pointer  Read this declaration ‘backwards’:  1 can contain the address of an integer variable  1 can point to an integer variable Alternative declarations 4 *1 * // * , // , * // * // * // Assign value to a Pointer   in front of variable return memory address of the variable Usually, we don't care what the address value of is. every time this program run, is the same. 5 , *1, *2 // 1 & // 2 1 // & Dereferencing a Pointer  To access the variable or memory location that the pointer points to * and & are complements of each other *&x => x 6 , *1, *2 // 1 & // 2 1 // *1 // ("%", * 2) //

description

Pointer

Transcript of 02 Pointers

  • #02 Pointers242-207 FUNDAMENTAL PROGRAMMING I

    DEPARTMENT OF COMPUTER ENGINEERING, PSU

    1

    v1.1

    Contents

    Introduction to Pointer

    Call by Reference

    Pointer & Array

    Pointer & Structure

    2

    Pointers

    To code Call by Reference

    To build lists, queues, stacks, trees, etc.

    Overview:

    Recap: Variable is just a box in memory.

    3

    x

    ptr1 ptr2

    Declaring a Pointer

    Read this declaration backwards:

    can contain the address of an integer

    variable

    can point to an integer variable

    Alternative declarations

    4

    !

    "

    ""

    "!!"

    Assign value to a Pointer

    in front of variable return memory address of the

    variable

    Usually, we don't care what the address value of

    is.

    every time this program run, is the same.

    5

    Dereferencing a Pointer

    To access the variable or memory location that

    the pointer points to

    * and & are complements of each other

    *&x => x

    6

  • Value vs. Reference 7

    #$

    %

    &

    $

    '(

    &

    #$

    %

    &

    " $

    (

    &

    Call by Reference 8

    main:

    allocate memory for a

    a = 3;

    call inc() with &a

    inc:

    allocate memory for ptr

    ptr = &a; (somehow)

    *ptr +=1;

    main:

    ...

    #$

    %

    &

    " $

    (

    &

    Array

    Pointer (Point-of-View)

    Automatically allocate memory for 5 integers

    Create a as a constant pointer

    a point to the beginning of the array

    a[0], *a gives you 0

    9

    0 1 2 3 4

    a

    (constant pointer)

    Passing array to functions 10

    #$

    )%*$&

    #!

    +++

    &

    "#! )*$

    )*,

    &

    0 1 2

    a

    a is known in main()

    b b is known in modify()

    Pointer Arithmetic

    11

    0 1 2 3 4

    a

    (constant pointer)

    p q

    0 1 2 3 4

    q q

    Case Study - Summation

    Pattern I: General style

    '# $

    -

    - .(($

    ()*

    &

    '

    &

    12

  • Case Study - Summation

    Pattern II: Pointer declaration

    '# $

    -

    - .(($

    ()*

    &

    '

    &

    13 Case Study - Summation

    Pattern III: Pointer arithmetic

    '# $

    -

    - .(($

    (

    &

    '

    &

    14

    Case Study - Summation

    Pattern IV: Pointer arithmetic, pointer sliding

    '# $

    -

    - .(($

    (

    &

    '

    &

    15 Case Study - Summation

    Pattern V: Pointer comparison

    '# $

    -

    $

    (

    &

    '

    &

    16

    0 1 2 3

    a b

    DO NOT Confuse!

    )%*

    !

    )%*

    !

    17 Pointer to Structure

    ' $

    !

    &

    ! ' /

    #$

    /

    /

    0

    &

    Declaration: same as

    pointer to primitive types

    -> is a special operator

    to access structure

    member from pointer.

    18

    + + 12

    +! +! 12!

  • Case Study: Line Distance 19

    #$

    /$&

    /$-&

    ++++++

    &

    //$

    123 12

    ! 12!3 12!

    '4(!!

    &

    DANGER! 20

    /

    12

    /

    /

    12

    /)%*

    )*12

    /

    /)%*

    )-*

    )*

    )*

    )*12