Page 160 - DCAP507_SYSTEM_SOFTWARE
P. 160

System Software




                    Notes               REG NO.     NAMECOURSE       TELEPHONE
                                   STDRECà
                                        {     {     {          {
                                        Field Field Field      Field

                                    The type and variable declaration for the above record can be done as follows:
                                   C:
                                   Struct STDREC               ] ® Structure-type-identifier
                                   {

                                              int REGNO;
                                              char NAME [15];
                                              char course [8];       ® members
                                              int TELEPHONE;

                                   }          SR1, SR2, SR#;         ] ® List of structure variables.

                                   ARRAYS

                                   So far, we've been declaring simple variables: the declaration
                                        int i;
                                   declares a single variable, named i, of type int. It is also possible to declare an array of several
                                   elements. The declaration
                                          int a[10];
                                   declares an array, named a, consisting of ten elements, each of type int. Simply speaking, an
                                   array is a variable that can hold more than one value. You specify which of the several values
                                   you're referring to at any given time by using a numeric subscript. (Arrays in programming are
                                   similar to vectors or matrices in mathematics.)
                                   In C, arrays are zero-based: the ten elements of a 10-element array are numbered from 0 to 9. The
                                   subscript which specifies a single element of an array is simply an integer expression in square
                                   brackets. The first element of the array is a[0], the second element is a[1], etc. You can use these
                                   "array subscript expressions" anywhere you can use the name of a simple variable.


                                          Example:
                                          a[0] = 10;

                                          a[1] = 20;
                                          a[2] = a[0] + a[1];
                                   Notice that the subscripted array references (i.e. expressions such as a[0] and a[1]) can appear on
                                   either side of the assignment operator.
                                   The subscript does not have to be a constant like 0 or 1; it can be any integral expression.


                                          Example: it's common to loop over all elements of an array:





          154                               LOVELY PROFESSIONAL UNIVERSITY
   155   156   157   158   159   160   161   162   163   164   165