Page 71 - DCAP201_FUNDAMENTALS_OF_DATA_STRUCTURES
P. 71

Fundamentals of Data Structures




                    Notes          An equivalent declaration is : int strlen(char *s); since char s[]  ≡  char *s.
                                   strlen() is a standard library function that returns the length of a string. Let’s look at how we may
                                   write a function:
                                    int strlen(char *s)
                                                  { char *p = s;


                                                                while (*p != ‘\0);
                                                                p++;
                                                                return p-s;
                                                  }
                                   Now lets write a function to copy a string to another string. strcpy() is a standard library
                                   function that does this.
                                    void strcpy(char *s, char *t)
                                                  { while ( (*s++ = *t++) != ‘\0);}
                                   This uses pointers and assignment by value.

                                   Self Assessment

                                   Fill in the blanks:

                                   1.  A ..................... is a variable which contains the address in memory of another variable.
                                   2.  The ..................... operator * gives the “contents of an object pointed to by a pointer”.
                                   3.  When we increment a pointer we increase the pointer by one “..................... ” memory.
                                   4.  When an array is passed to a function what is actually passed is its initial elements location
                                       in ..................... .
                                   5.  ..................... is a standard library function that returns the length of a string.

                                   5.2 Arrays of Pointers


                                   We can have arrays of pointers since pointers are variables.
                                   An array of pointers can be declared as :
                                   <type> *<name>[<number-of-elements];


                                          Example:
                                   char *ptr[3];

                                   The above line declares an array of three character pointers.

                                       !
                                     Caution  Text can’t be moved or compared in a single operation.
                                   Arrays of Pointers are a data representation that will cope efficiently and conveniently with
                                   variable length text lines.







          64                                LOVELY PROFESSIONAL UNIVERSITY
   66   67   68   69   70   71   72   73   74   75   76