Pointers 1

Embed Size (px)

Citation preview

  • 7/29/2019 Pointers 1

    1/5

  • 7/29/2019 Pointers 1

    2/5

    Hence it is preceded by * . Variable p

    also contains address, since p is also a

    variable.

    Contents of variable a can be accesed

    by a or *p . *p means contents of the

    memory of a.

    Note :- * means contents of

    & means address of

    Address of operator :- & is called address

    of operator. Any variable preceded lby &

    indicates the address of memory location.

    Ex:- int b ;

    &b gives some address in whichcompiler allocates for a variable b 1010.

    Ex :- int a=5 ;

    Int *p=&a ;

    Printf(%d,a); gives output 5

    Printf(%d,*p); gives output 5

    Printf(%d,&a); gives output -42

    Printf(%d,p); gives output -42

    (address of a)

  • 7/29/2019 Pointers 1

    3/5

    Printf(%d,&p); gives output -40

    (address of p)

    Note :- address always takes unsigned

    integer, thus pointer variable always tekes

    2 bytes of memory inrrespective of its

    data type.

    Declaration of a double pointer :- double

    pointer is same as similar to the pointer,

    but it sotres address of another pointervariable.

    Ex:- int a = 5 ;

    int *p = &a ;

    int **p = &p ;

    where a is a variable whose content is 5

    and contains some address as -42 , p is a

    pointer variable which stores the address

    of a also p contains some address as -40.

    q is a double pointer variable which

    stores the address of pointer variable alsoq contains some address as -38.

    Ex :-

    /* Example Program */

  • 7/29/2019 Pointers 1

    4/5

    /* To print the value and address of

    pointer variables and normal variables */

    #include

    #include

    main()

    {

    int a=5,*p=&a,**q=&p;

    clrscr();

    printf("a=%d\n",a); /* gives output

    5 */

    printf("*p=%d\n",*p);/* gives output 5

    */

    printf("&a=%d\n",&a); /* gives output

    -42 */

    printf("p=%d\n",p);/*gives output -42

    (address of a) */

    printf("&p=%d\n",&p);/*gives output-40(address of p*/

    printf("q=%d\n",q);/* gives output -40

    (address of a) */

  • 7/29/2019 Pointers 1

    5/5

    printf("&q=%d\n",&q);/*gives output

    -38(address of p)*/

    printf("**q=%d\n",**q); /* gives output

    5 */

    getch();

    }