12 pointers

Upload: adityabaid4

Post on 03-Apr-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/28/2019 12 Pointers

    1/22

    27/03/06 1

    POINTERS

  • 7/28/2019 12 Pointers

    2/22

    27/03/06 2

    Declaration a point about pointers

    int j, k, l;

    j, k, l are integer type. we can say that int type declaration distributes

    over the list of names j, k, l

    int *p, q, r; Here, only p is int pointer

    q and r are ordinary int variables

    The indirection operator (*) does not distribute toall variable names in a declaration. Each pointermust be declared with the * prefixed to the name.

  • 7/28/2019 12 Pointers

    3/22

    27/03/06 3

    Pointing to nothing !

    NULL is a symbolic constant defined in

    with 0 (zero) A pointer with the value NULL points to

    nothing.

    The value 0 (NULL) is the only integer valuethat can be assigned directly to a pointervariable.

    int *p = NULL;

    *p = 5; /* this results in run-time error */

  • 7/28/2019 12 Pointers

    4/22

    27/03/06 4

    Functions

    A function can return only one value back to the

    calling function.

    If you want more values to be communicated

    back to the calling function, then you have to useaddresses and pointers.

  • 7/28/2019 12 Pointers

    5/22

    27/03/06 5

    Functions an examplevoid find_min_max_marks(float *, float *, float *, int);

    main( )

    {

    float min, max, marks[40];

    ;

    find_min_max_marks(marks, &min, &max, 40);

    }

    void find_min_max_marks(float *m, float *a, float *z, int size)

    {

    int j;

    *z = m[0]; *a = m[0];

    for (j=1; j *z) *z = m[j];

    }

    }

  • 7/28/2019 12 Pointers

    6/22

    27/03/06 6

    Comparing two strings

    strcmp(s, t) is a function available in the

    standard library () that comparesthe character strings s and t.

    It returns an integer which is 0 (zero) s and t are same Negative s is lexicographically less than t

    Positive s is lexicographically larger than t

    The return value is obtained by subtractingthe characters at the first position where s

    and t disagree.

  • 7/28/2019 12 Pointers

    7/22

    27/03/06 7

    strcmp array version

    int strcmp(char *s, char *t)

    {

    int j;

    for(j=0; s[j] == t[j]; j++)

    if( s[j] == \0) return 0;

    return s[j] t[j] ;}

  • 7/28/2019 12 Pointers

    8/22

    27/03/06 8

    strcmp pointer version

    int strcmp( char *s, char *t)

    {

    for ( ; *s == *t; s++, t++)

    if(*s == \0) return 0;

    return *s - *t;

    }

  • 7/28/2019 12 Pointers

    9/22

    27/03/06 9

    const with pointers

    Passing addresses to functions could be

    dangerous. By mistake that function canmodify the original variables value.

    To overcome this kind of problems, one candeclare that the contents pointed by thepointer are constant.

    One can also force to say that a pointer hasto point to the same location (whatever becontents of the location).

  • 7/28/2019 12 Pointers

    10/22

    27/03/06 10

    const with pointers

    const can be used in 4 ways with pointers

    a non-constant pointer to non-constant data a constant pointer to non-constant data

    a non-constant pointer to constant data

    a constant pointer to constant data

  • 7/28/2019 12 Pointers

    11/22

    27/03/06 11

    A non-constant pointer to non-

    constant data int *p, j = 10, k = 20;

    p = &j;*p = 15; /* contents pointed by p is modified */

    p = &k; /* p is modified */

  • 7/28/2019 12 Pointers

    12/22

    27/03/06 12

    A constant pointer to non-constant

    data Pointer always points to the same memory

    location. But contents at that memory canchange.

    int x, y;

    int * const ptr = &x;

    *ptr = 7; /* OK same as x = 7*/

    ptr = &y; /* error */

  • 7/28/2019 12 Pointers

    13/22

    27/03/06 13

    A constant pointer to non-constant

    data An array name is a constant pointer to the

    beginning of the array.

    All the data in the array can be accessed andchanged by using the array name and array

    subscripting. But array name cannot point to some other

    location.

  • 7/28/2019 12 Pointers

    14/22

    27/03/06 14

    A non-constant pointer to constant

    data pointer can be modified to point to some

    other location. But the contents using the pointer (with

    indirection operator *) can not be modified.

    This is especially useful with functions.

  • 7/28/2019 12 Pointers

    15/22

    27/03/06 15

    A non-constant pointer to constant

    data const int * ptr;

    int j = 10, k;ptr = &j; /* OK */

    *ptr = 25; /* error */

    j = 25; /* OK */

    ptr = &k; /* OK */

    *ptr = 100; /* error */k = 100; /* OK */

    j = *ptr; /* OK */

  • 7/28/2019 12 Pointers

    16/22

    27/03/06 16

    A non-constant pointer to constant

    data void f( const int *);

    int main( )

    {

    int y = 111;

    f(&y);

    }

    void f( const int * ptr)

    {

    *ptr = 100; /* an error occurs */

    }

  • 7/28/2019 12 Pointers

    17/22

    27/03/06 17

    A constant pointer to

    constant data int x = 5, y;

    const int *const ptr = &x;y = *ptr; /* OK */

    ptr = &y; /* error */

    *ptr = 20; /* error */

    If you want to pass an array whose contentsshould not be modified, then use this kind ofdeclarations.

  • 7/28/2019 12 Pointers

    18/22

    27/03/06 18

    Pointer Expressions and

    Pointer Arithmetic A limited set of arithmetic operations may be

    performed with pointers A pointer may be incremented (++) or

    decremented ( -- )

    An integer may be added to a pointer (+ or += ) An integer may be subtracted from a pointer

    ( or = )

    One pointer may be subtracted from another.

    Pointer arithmetic is machine dependant.

  • 7/28/2019 12 Pointers

    19/22

    27/03/06 19

    Increment ++ and decrement --

    int *ptr, j = 5;

    int a[20];ptr = a;

    ptr ++; /* ptr now points to a[1] */

    *ptr = 10; /* same as a[1] = 10 */

    ptr = &j;

    ptr ++;/* now ptr points to next integer after j */

    Pointer arithmetic is meaningful only with arrays

  • 7/28/2019 12 Pointers

    20/22

    27/03/06 20

    Subtracting a pointer from other

    double d[20];

    double *a, *b;int j;

    a = &d[4];

    b = &d[10];

    j = b a; /* j gets value 6 */

    Again, it would be meaningless if a and b arepointing to non-array elements.

  • 7/28/2019 12 Pointers

    21/22

    27/03/06 21

    Assigning a pointer to another

    Void pointer A pointer can be assigned to another pointer

    if both pointers are of the same type. Otherwise, a type cast operator must be

    used.

    int *p; char *c;

    p = (int *) c;

    The exception to this rule is for pointer whosetype is void *

  • 7/28/2019 12 Pointers

    22/22

    27/03/06 22

    void pointervoid pointer

    void *ptr; int i;

    ptr = &i; Here ptr can contain an address, but the type

    of the value stored at that address is

    unknown and hence, dereferencing using ptris disallowed.

    *ptr = 5; /* error */

    *((int *)ptr) = 5; /* Ok, type cast and thenassign*/