***Welcome to ashrafedu.blogspot.com * * * This website is maintained by ASHRAF***

Posts

    Tuesday, November 24, 2020

    Pointers in C

    pointer is a variable that stores the address of another variable. Unlike other variables that hold values of a certain type, pointer holds the address of a variable. 

    A pointer provides a way to access a variable without referring to the variable directly.

    Restriction on pointers is they are not allowed to store any memory address, but they can only store addresses of variables of a given type.

     Consider the following example to define a pointer which stores the address of an integer.

             int n = 10;   

       int* p = &n; // Variable p of type pointer is pointing to the address of the variable n.

    The pointer in c language can be declared using * (asterisk symbol). It is also known as indirection pointer used to dereference a pointer.

    The ‘*’ operator (indirection operator) is used in two distinct ways with pointers

    -          used for pointer declaration.

    -          used for dereferencing.

    Used as dereferencing operator ‘*’ indicates that the value at the memory location is to be accessed.

     Example:

    void main( )

    {

                int a=10;

                int *p;

                p= &a;

                printf(“value of a %d”, a);

                printf(“value of a %d”,*p);

                 printf(“address stored in p is %u”, p);

    }   

     

    Features of Pointers:


    1. Pointers save memory space.
    2. Execution time with pointers is faster because data are manipulated with the address, that is, direct access to memory location.
    3. Memory is accessed efficiently with the pointers. The pointer assigns and releases the memory as well. Hence it can be said the Memory of pointers is dynamically allocated.
    4. Pointers are used with data structures. They are useful for representing two-dimensional and multi-dimensional
      arrays.
    5. An array, of any type can be accessed with the help of pointers, without considering its subscript range.
    6. Pointers are used for file handling.
    7. Pointers are used to allocate memory dynamically.

     

    No comments:

    Post a Comment

    Top