esc101-lec20

Upload: mukesh-kumar-dewra

Post on 05-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 esc101-lec20

    1/6

    1

    ESc101: Fundamentals of Com utin

    2011-12-Monsoon Semester

    Lecture #20, September 19, 2011

    Please switch off your mobile phones.

    Announcements

    Mid-sem exam co ies will be returned in Wednesda Tutorial.

    Lec-20 1Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

  • 7/31/2019 esc101-lec20

    2/6

    2

    Recap

    String fucntions

    Array initialization

    Lec-20 2Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    Recap: Memory Model

    Memory is a list ofbytes

    Each byte has an address

    All variables are stored in memory

    As many bytes are allocated to the variable as the type ofthat variable requires

    Memory for a single variable is contiguous

    o guarantees w et er two consecut ve y ec are

    variables will get contiguous memory areas (bytes)

    Address of a variable is the address of the first byte where

    it is stored

    Lec-20 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    3

  • 7/31/2019 esc101-lec20

    3/6

    3

    Recap: Memory Model

    ci

    0 1 2 3 4 5 6 7 8 9 10 12

    int i;

    char c;

    Lec-20 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    4

    Address of i is 1. It occupies bytes 1 to 4.

    Address of c is 7.

    It only occupies byte 7.

    Pointer

    variable.

    A pointer is denoted by the symbol *

    int i; // i is a variable of type int

    int *p; // p is a pointer to some integer

    The address of a variable is denoted by the symbol &

    p = &i; // p is now address of i (1 in our example)

    p stores the address of i, i.e., &i

    *p denotes the content pointed by p, i.e., i.

    Lec-20 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    5

  • 7/31/2019 esc101-lec20

    4/6

    4

    Why we need to know the addresses

    ,

    the same memory areas in both callee and called functions.

    Like what happened with arrays

    We did not have to copy large arrays during function calls

    When we want the changes made to memory areas in

    called functions to be reflected in callee function, knowing

    .

    We will also learn about better memory management by

    asking computer to allocate only as much memory as

    needed at the run time through the use of pointers.

    Lec-20 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    6

    Have we seen the pointers before

    When a variable is read using scanf ( )

    Operator & gives location where scanf ( ) should store value

    Notice the difference between scanfand printf:

    We wanted the parameter variable to change after scanf The changes made within scanfshould be visible in the callee

    function

    Hence we passed on the address of the variable and not the content

    or va ue o e var a e

    In printf, we did not want the function to make any changes

    The value should simply be printed and no changes to be done

    Hence only the content or value of the variable needed to be passed

    Lec-20 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    7

  • 7/31/2019 esc101-lec20

    5/6

    5

    Have we seen pointers before

    Arrays are passed as address

    Array names are essentially pointers

    a is a pointer to the first element of the array, i.e., a[0]

    *a is equivalent to a[0]

    Since array elements are stored contiguously in memory

    (a + i) is a pointer to a[i]

    *(a + i) is equivalent to a[i]

    Notice that pointer arithmetic is different from normal

    arithmetic

    When we add 1 to a, we are actually telling the computer to get

    the address of the next integer (or whatever type array is of)

    Lec-20 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    8

    More on pointers and arrays

    ,

    of (a + i) and accesses the variable in that address

    If the computer has 4-byte integers, then (a+i) would

    actually translate to a + 4i, where a is the address of a[0] No error checking on array boundaries is done

    Therefore, a[-2], a [12], etc., become legal in C language

    thou h ma be wron as er the lo ic of our ro ram

    Pointers can be assigned array names:

    int *p;

    p = a;

    Lec-20 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    9

  • 7/31/2019 esc101-lec20

    6/6

    6

    Swapping two integers

    void swap (int *pa, int *pb)

    {

    int t = *pa;

    * = *

    *pb = t;

    }

    int main ( )

    {

    int x = 3, y = 4;

    int *a, *b;

    = = , , ,

    a = &x; b = &y;

    swap (a, b);

    printf(x = %d, y = %d\n, x, y);

    }

    Lec-20 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    10

    Any Questions?

    Lec-20 11Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon