Page 67 - DCAP407_DATA_STRUCTURE
P. 67

Data Structure



                          invalid  addresses might cause the system to crash. Pointers used  incorrectly can cause bugs  and is
                          difficult to identify.
                          4.1   Fundamentals of Pointers

                          The address which locates a  variable  within the memory is a  reference to that variable and can be
                          obtained by preceding the variable name with an ampersand (&) or the reference operator.


                                              x = & a;

                          This would assign the address of a to x. Since, & is a reference operator, it stores the address of the
                          memory and not the content of the variable.


                                              a= 30;
                                              b= a;
                                              x= &a;
                                              Here, b will contain the value of a, whereas x will contain the address of a.
                          Pointer can be declared as:

                          type * variablename;
                          Here, type is the data types of the value like int, char, and float that the pointer points to.

                                            int *a;
                                            char *name;
                                            float *username;
                                            *variablename is used to point to a value that the pointer will point.
                                            An asterisk (*) acts as a dereference operator.


                                            void main ()
                                            {
                                                int iv, fv;
                                                int * m;
                                                m= & iv;
                                              *m= 5;
                                                m = &fv;
                                              *m= 10;
                                                 printf( “ Initial value is %d\n “, &fv;)
                                                 printf( “ Final value is %d\n”, &sv;)
                                            }
                                            Output:
                                            Initial value is 5
                                            Final value is 10
                                            In this example:
                                            1.   First, the address of iv is assigned to m using the reference operator (&).
                                            2.   Then, the value 5 is assigned to the memory location pointed by m because
                                                 at this moment, m is pointing to the location of iv, which modifies the value
                                                 of iv.
                                            3.   Then, the address of fv is assigned to m using the reference operator (&).
                                            4.   Finally, the value 10 is assigned to the memory location pointed to by m,
                                                 i.e., fv, using the dereference operator (*).






                          60                      LOVELY PROFESSIONAL UNIVERSITY
   62   63   64   65   66   67   68   69   70   71   72