Ad

Pointers

Pointers

 POINTERS


  • Memory:
    • 4 GB RAM => 4 * 2^30 BYTES
    • 1 KB => 2^10 BYTES
    • 1 MB => 2^20 BYTES
    • 1 GB => 2^30 BYTES
    • 1 TB => 2^40 BYTES
  • RAM is called Random Access Memory as if an address is given, it can access it directly whereas Files is an example of Sequential access, and to reach the 5th word in the 10th line of a file, it has to go through all the 10 lines in order to reach the 5th word on 10th line.
  • Addressing in (4GB) RAM starts from 0 to (4 * 2^30) - 1.
  • To study about the internal working of RAM, Computer Organisation and Operating systems are the subjects which would help.
    • int i;                          // declaration
    • When we declare an int variable, a memory of 2 bytes is allocated to i (if the size of an integer is 2 bytes). 
    • When we initialize the variable, it is stored inside the allocated location in binary form.
  • Operators related to pointers:
    • Address Operator (&): & is a unary operator which indicates the Address of / the memory location of Variable stored in the RAM. It is indicated by the symbol Ampersand (&).
      • e.g.        printf ( "\n %u" , &i );
                   The above line will print the address of i, which in the above segment is 2000.
    • Address of Operator can be used with variables but it cannot be used with constants.
      • e.g.        &i                    is valid
                                    &236               is invalid
    • Address of Operator cannot be used with expressions.
      • e.g.               &(i+k)            is invalid
    • Dereferencing/Indirection Operator (*): * is a unary operator also known as Dereferencing/Indirection Operator which is represented by Asterisk (*). This concept is explained in more detail below.
  • A pointer is a variable that stores an address.
    • e.g.              int *pint;
            Here, pint is a pointer variable of integer type, which will store the memory address of integer variables.
    • e.g.              float *pf;
            Here, pf is a pointer variable of float type, which will store the memory address of float variables.
  • pint and pf are also variables, hence they will also have their memory address just like other variables. The size of the pointer variables is machine-specific.
  • Declaration of Pointer variables:
    •        data-type   *pointer-name ;
    •        int *ptr , x = 30 ;
  • Assignment/Initialization of Pointer variables:
    •        pointer-name  = &variable-name ;
    •        ptr = &x ;
  •                    float f = 1.0 ;
                          pint = &f ;
    • In the above case, it will not give us a compile-time error, but it would give us a wrong output logically, as the pint is a pointer of integer type whereas we are trying to store the memory location of a float variable.
  •                    int *pint2 = pint ;
    • In the above case, a pointer variable will be created in the memory location and the value stored in pint (i.e. 2000- the address of x) would be copied in pint2.
  •                    int *pint = NULL ;
                                    or
                          int *pint = 0 ;
    • In the above case, NULL is a constant which is present in stdio.h which represents 0 internally. In the above case, the pint is pointing towards NULL OR 0 which also means that the pointer is pointing to nothing.
  • Dereferencing/Indirection Operator (*): * is a unary operator also known as Dereferencing/Indirection Operator which is represented by Asterisk (*).
    •                             int x = 10 ;
                                          int *pint = &x ;

                                         *pint = 9 ;


    Here, * is the indirection Operator. You can read * as the value stored at the memory location. The third line indicates, the value stored at pint = 9, which means value stored at address 2000 = 9 i.e. x = 9;
    •                            (*pint)++
    Here (*pint)++ means x++ as pint can be written as *(&x)++ which means *(2000)++, *2000 means the value present at the memory address 2000, which gives us 9++ which is 10.
    •                            printf ( "\n %d" , *pint ) ;
    Here, *pint means the value present at the address stored in pint, which means it will print the value of x ie. 10.
    •                           scanf( "%d" , pint) ;
    Here, pint means &x, hence the above statement will input the value into x.
    • * and & cancel each other when they are together. 
    e.g.            *(&x) is the same as x because *(&x) means value stored at the address of the location of x.
  • We define the data type of a pointer variable to allocate different amounts of memory to different types of data.
    •                           float *pf ;
                                        float f = 1.0

                                        printf ( "%u" , *pf ) ;


    Here, if we make pointer pf of integer data type, then it will read from address 2000, but if the change the data type of pointer variable, then, the pf pointer will not know whether to read 2 bytes or 4 bytes or so from the address of 'f'. The moment when we declare the pointer variable as float data type, it knows it has to read 4 bytes from the assigned address.
Explanation:
Because it is designed to point to float variables, whenever it is being dereferenced using the indirection operator, it should obtain 4 bytes of memory starting at the address assigned to it.
  •                      int *ptr , x = 10 ;
                            ptr = &x ;

                            sizeof ( ptr ) ;

                            sizeof ( *ptr ) ;

    In the above lines of code, sizeof(ptr) is different from sizeof(*ptr) as sizeof(ptr) means the size/memory required to store the pointer variable ptr whereas, sizeof(*ptr) means the size of the value stored at ptr which means value stored at &x which is equal to sizeof(int).

  • We can print pointers using %p which represents hexadecimal value.

  • Pointer Arithmatic
    •               int x = 10 ;
                            int *ptr = &x ;

    
    Here, ptr+1 => 2000 + 1* (sizeof(int))
    Therefore,     
    pointer_name+n    =>   *pointer_name + n*(sizeof(pointer_data_type))

    e.g.    float f = 1.0 ;
             float *pf = &f ;
    Here,   pf + 1 => 3000 + 1 * (sizeof(float))

  • Valid Arithmetic Operations on Pointers:
    • pointer + constant 
    • pointer - constant
    • pointer++
    • pointer--
    • ++pointer
    • --pointer
    • pointer1 - pointer2
  • Invalid Arithmetic Operations on Pointers:
    • pointer1 + pointer2
    • pointer1 pointer2
    • pointer1 pointer2
    • pointer constant
    • pointer constant
    • pointer float or double constant
    • pointer * float or double constant
    • pointer / float or double constant
    e.g.    ptr - 2 => 2000 - 2*(2) => 1996
              pf - 2 => 3000 - 2*(4) => 2992


  • Precedence of Operators on Pointers:
    • ++ (post and pre increment), -- (post and pre increment), *, &
    All the above operators have Right to Left associativity.

                     int x , y ;
                  x = 10 ;

                  y = 12 ;

                  int *ptr = &x ;

    • Here, if we write x = *ptr++, as the unary operators associativity is from right to left, x = *ptr++ is same as x = *(ptr++) which is equal to x = 10 and *ptr is incremented.
    • Here, if we write x = *++ptr, as the unary operators associativity is from right to left, x = *++ptr is same as x = *(++ptr) which is equal to x = 12.
    • Here, if we write x = (*ptr)++, as the unary operators associativity is from right to left, x = (*ptr)++ is same as x = (*ptr)++ which is equal to x = 11 and *ptr is incremented.
    • Here, if we write x = ++*ptr, as the unary operators associativity is from right to left, x = ++*ptr is same as x = ++(*ptr) which is equal to x = 11 and *ptr  is incremented.
  • Comparison between pointers
    Following operators can be used for comparison between pointers:
    • ==
      • If both pointers are NULL or pointing towards the same memory location (and the data type of both the pointers are the same), == operator will return true and vice versa.
      • You can compare void pointer with other pointers of any data type.
      • You can compare NULL pointer with other pointers of any data type.
    • !=
      • If both pointers are NULL or pointing towards the same memory location (and the data type of both the pointers are the same), == operator will return false and vice versa.
    • <
      • For < operator, both the pointers have to be of the same data type
    • <=
      • For <= operator, both the pointers have to be of the same data type
    • >
      • For > operator, both the pointers have to be of the same data type
    • >=
      • For >= operator, both the pointers have to be of the same data type
  • Pointers to Pointers
    •   int i = 10;
                int *pi = &i ;

                int **ppi = &pi ;
 
    Here ppi is a pointer to a pointer which means it would store the address of a pointer variable. It is also known as a double-pointer.
    SYNTAX:            data_type    **pointer_name ;
    • i   =>    *p   =>    **ppi
    • &i   =>    pi   =>    *ppi

0 Response to "Pointers"

Post a Comment

If you have any doubts, please let me know...

Ads Atas Artikel

Ads Center 1

Ads Center 2

Ads Center 3