Page 69 - DCAP407_DATA_STRUCTURE
P. 69

Data Structure



                          When a pointer is declared, C language allocates space for the pointer. There are many functions which
                          can be used for dynamic memory allocation and deallocation. They are:
                          1.  Malloc(): This function allocates a block of memory. A pointer of type void is returned when a
                              block of memory with a specific size is reserved. This function can then be assigned to any type of
                              pointers.

                          2.   Calloc(): This function allocates same sized multiple blocks of storage, when required.
                          3.   Realloc():  This function moves reserved block of memory to another location of  different
                              dimensions. This is used to change the memory allocated by  functions,  like  calloc and malloc,
                              when the memory is either insufficient or is in excess.
                          4.   Free(): This function releases a previously used block of memory.


                                      Malloc() function is used when a pointer has to point to a block of memory and calloc()
                                      is used to request the memory space for an array which is initialized with zero-value
                                      blocks.

                          Consider the following pointer declaration:
                          char *y;
                          The variable type for y is declared to be of pointer to char. The pointer y must be initialized at the time
                          of its declaration to avoid the chances of random value being used as memory address:
                          char *y = NULL;


                          4.2   Operations on Pointers
                          We can use pointer variables for various types of expressions. The table 4.1 specifies the  operators
                          which can be used using pointers.

                                                         Table 4.1: Pointer Operations



                            Operators name             Symbols                    Examples
                                                                                  (x and y are pointer variables)
                            Relational operator        >                          x>y
                                                       >=                         x>=y
                                                       ==                         x==y
                                                       <                          x<y
                                                       <=                         x<=y
                                                       !=                         x !=y

                            Increment operator         ++                         x++
                            Decrement operator         --                         y--



                          The above mentioned operators such as, >=, <=, >, <, ==, !=, ++, and -- can be applied to pointers only
                          when both operands are pointers.


                                           int *a, *b;
                                           If (a >= b)
                                           {
                                           }




                          62                      LOVELY PROFESSIONAL UNIVERSITY
   64   65   66   67   68   69   70   71   72   73   74