9 pointers

Upload: syukz

Post on 04-Jun-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 9 Pointers

    1/12

  • 8/14/2019 9 Pointers

    2/12

    Pointers

    y &%%(

    &%%)

    &%%

    &%%/

    ch '%%%

    When these variables are assigned values, for e*ample,

    a=5, b=12, c= 67;

    x= 12.45, y=45.7;

    ch= G;

    these values will be stored in memory as shown.

    a $%%%

    $%%$

    b $%%&

    $%%'

    c $%%(

    $%%)

    * &%%%

    &%%$&%%&

    &%%'

    y &%%(

    &%%)

    &%%

    &%%/

    ch '%%% G

    "he memory addresses of the variables can be obtained using the 0 1address of2 operator.

    "he address of variable ais denoted by &awhich is $%%%. -or e*ample, to determine the

    address of variable a, we can use a variablepaand assign the address to it as in the

    e*pression#

    pa = &a;

    3imilarly, for addresses of other variables, we can write

    pb = &b;

    ++

    chartype occupies $byte.

    5

    12

    67

    12.45

    45.7

  • 8/14/2019 9 Pointers

    3/12

    Pointers

    pc = &c;px = &x;pch = &ch;

    "he statement

    pa=&a; /*pa contain th! a""r! of a */

    assigns the address of ato variablepa4pais now 5pointing6 to a.pais called a pointer a

    variable that contains the address of another variable. "he value of acan be accessed by the

    e*pression *pa, where *is a unary operator, called the indirection operator. 7t operates only

    on a pointer variables.

    "he following printf12 statements prints the value of a

    printf #$%",a'; /* print th! (al)! of a */printf#$%",*pa'; /* print th! (al)! of a too */

    9.2 Declaration of pointers

    8ll variables have to be declared before they can be used in a function, so are pointer

    variables. Pointer variables tae on the data types of the variables that they are used to point

    to. -or e*ample,papoints to a which is an integer value. 9ence the declaration forpais

    int *pa;

    3imilar for other variables listed above, we have

    int *pb, *pc;float *px, *py;char *pch;

    "he program below illustrate the relationship between an integer variable and its associated

    pointer.

    /* ro+ra -1 */

    incl)"!0t"io.h

    ain#' int x= 13; int *px;

    px=&x;printf #$h! (al)! of x i %",*px';

    x; /* incr!!nt x */#*px'; /* incr!!nt x in"ir!ctly */

    printf #$h! (al)! of x i no %",x';

    $%%

  • 8/14/2019 9 Pointers

    4/12

    Pointers

    "he screen printout of the above program will be#

    9.3 Pointers as argments in a fnction

    We have seen that when a function is called by value, the called function does not alter the

    value of the variable it is only the value of the variable that is supplied to the function and

    not the variable itself. "o illustrate the concept again, let loo at the ap#'function below.

    "he ap#'function swaps the values of two variables.

    /* ro+ra -2 8 9 pro+ra that "o! not "o it :ob */

    incl)"! 0t"io.h

    ain#'

    int a= a=%" an" b = %"?n, a,b';

    /* f)nction "!finition */

    (oi" ap#int a, int b'

    int t!p;

    t!p = a;a=b;b=t!p;

    "he printout will be

    "here is no swap at all: When the function is called by value, the data item is copied to the

    function. 8ny alteration done in the function is not carried over to the calling routine. "o

    solve the ap#'function problem, we can pass the addresses of the variables to function.

    "he address of a variable, once allocated, does not change. We can, then, use pointers to

    point to these variables and swap their values. 9ence the swap function should be modified as

    shown.

    $%$

    h! (al)! of x i 13h! (al)! of i no 12

    !for! appin+> a = < an" b = 59ft!r appin+> a = < an" b = 5

  • 8/14/2019 9 Pointers

    5/12

    Pointers

    ap#pa, pb'int *pa, *pb;

    int t!p;

    t!p = *pa;*pa = *pb;*pb = t!p;

    "he modified Program +;& will be as shown#

    /* ro+ra -< 8 hi pro+ra or@ */

    incl)"! 0t"io.h

    ain#' int a= a=%" an" b = %"?n,a,b';

    /* f)nction "!finition */

    (oi" ap#int *pa, int *pb'int t!p;

    t!p = *pa;*pa=*pb;*pb=t!p;

    "he screen printout of this program

    "he swap()function is not called by valuesbecause the values of variable aandbare not

    passed to it. What are passed to the function are the addressess of these variables and the

    swap act on the contents of these addressess. "he function is said to be called by reference.

    $%&

    8ddresses of variables

    a and b are passed into

    the function

    Need to declarepointer variablespa

    andpbinmain()

    andassign addressess of

    variables to them.

    !for! appin+> a = < an" b = 59ft!r appin+> a = 5 an" b =

  • 8/14/2019 9 Pointers

    6/12

    Pointers

    oes the program swap the variables?

    & =nter and run Program +;'. >oes the program swap the variables?

    ' What is the meaning of each of the following declarations ?

    1a2 int a,b; 1d2 float a=2.

  • 8/14/2019 9 Pointers

    7/12

  • 8/14/2019 9 Pointers

    8/12

    Pointers

    the computer system allocates a base address and a sufficient memory locations for seven

    elements. 3uppose the system allocates the base address of )%%% for the above array. i.e.

    address dayD%E is )%%% or 0dayD%EF)%%%, 0dayD$EF)%%&. Recall that the int type data is &

    bytes long.

    dayD%E dayD$E dayD&E dayD'E dayD(E dayD)E dayDE

    )%%% )%%& )%%( )%% )%% )%$% )%$&

    "he statements

    p"ay = &"ayF3 and p"ay = "ay

    are eAuivalent because the array name day is an 8>>R=33

    "ry this little program out.

    /* ro+ra -4 8 9r! th!y th! a!H */

    incl)"! 0t"io.h

    ain#'

    int "ay F7, *p"ay;

    p"ay = &"ayF3;

    printf #$p"ay = %) an" "ay= %)?n, p"ay,"ay';

    "he screen printout may be#

    Loo, they are the same. "hese numbers are computer dependent.

  • 8/14/2019 9 Pointers

    9/12

    Pointers

    "o summarised, an array and the pointer is one and the same thing. 8n array is actually a

    pointer but the address is fi*ed. When we use a pointer 1e.g.ptest2 to point to a variable,

    say test. "he assigment statement below is a must.

    pt!t = &t!t;

    otherwiseptestwill not now where to point to.

    Gut in array declaration, for e*ample

    int n)F ;

    the address of the base element is automatically assigned to numby the system; a fi*ed

    address.

    9.5 !"en do se pointers#

    Hse pointers when you want the function to act on variables that are defined in other

    functions.

  • 8/14/2019 9 Pointers

    10/12

    Pointers

    ptmphas the address value of )%%% and *ptmppoints to that address, i.e. tempD%E. Gy

    definition,ptmp+1points to the ne*t element 1tempD$E2 and has the address value of )%%&.

    i.e.

    ptmp+1F)%%& and this eAuivalent to temp +1.

    "hereforeptmp + kpoints to the thelement in the array.ptmp + kis eAuivalent totemp +k

    When we increment the pointer by $, @ adds $ storage unit 1i.e & bytes for intdata type nad

    ( bytes for floatdata type2.

    /* ro+ra -5 o print th! a""r! of (ariabl! */

    incl)"!0t"io.h

    ain#'

    char chF5 = $JK, *pch;

    int intCn)F5 = 13,23,';printf#$pch=%) pint=%) pflt=%)?n,pch,pint,pflt';

    printf#$?nincr!a! point!r by 1 an" th! a""r! ar!>?n';pch;pint;pflt;printf #$Lal)! of point!r >';printf#$pch=%) pint=%) pflt=%)?n,pch,pint,pflt';

    "he screen print out is as shown#

    "he above results shows that when a char pointer is increased by $, its value is increased by $

    1$ bytes2, when an int pointer is increased by $, its value is increased by & 1for & bytes2 and

    when a float pointer is increased by $, its value is increased by ( 1( bytes2.

    Note that these numbers are machine dependent4 when you run this program you may get a

    different set of values.

    $%/

    a! a""r!! of array >ch =6545I intCn)=65464 fltCn)= 65474Lal)! of point!r >pch=6545I pint= 65464 pflt= 65474

    9ft!r incr!ain+ point!r by 1 an" th! a""r!! ar!>

    Lal)! of point!r >pch=6545- pint= 65466 pflt= 6547I

  • 8/14/2019 9 Pointers

    11/12

    Pointers

  • 8/14/2019 9 Pointers

    12/12

    Pointers

    /* f)nction "!finition */

    (oi" )!rfn #int *p';

    int i, )=3;

    for #i=3 ; i0=5; i'

    ) =*#pi';

    printf #$) = %"?n,)';r!t)rn;

    1a2 What ind of argument is passed to userfn12?

    1b2 What ind of information is returned by userfn12?

    1c2 What is the purpose of the for loop that appears within the userfn12?

    1d2 What value is displayed by the printf12 statement within userfn12?

    **

    $%+