Page 96 - Programming Microcontrollers in C
P. 96

Multidimensional Arrays      81

                          that has much the appearance of a function called sizeof. To deter­
                          mine the size of any variable, use the sizeof operator as follows:
                   a = sizeof array;

                          which will return the number of bytes contained in array[][] above.
                              There are several important different ways that you can use the
                          sizeof operator. First, the value of the return from sizeof is in
                          characters. The type of the return is called a type size_t. This special
                          type is usually the largest unsigned type that the compiler supports.
                          For the MIX compiler, it is an unsigned long. If you should want
                          the size of a type in your program, you should enclose the parameter in
                          parentheses. If you want the size of any other item, do not use the paren­
                          theses. One other item. If the sizeof operator is used in a module
                          where an array is defined, it will give you the size of the array as above.
                          If you should pass an array to a function, the array name degenerates to
                          a pointer to the array, and in that case, the return from the sizeof
                          operator would give you the size of a pointer to the array.
                              A common example program using two-dimensional arrays is to
                          determine the Julian date. The Julian date is simply the day of the
                          year. The following function is one that allows counting the number
                          of days that have passed in a year.
                   int month_days[2][13] = {
                                {0,31,28,31,30,31,30,31,31,30,31,30,31},
                                {0,31,29,31,30,31,30,31,31,30,31,30,31}};
                   int Julian_data(int month, int date, int year)
                   {
                       int i,leap;
                       leap = year%4==0 && year%100!=0 || year%400==0;
                       for(i=1;i<month;i++)
                          day += month_days[leap][i];
                       return day;
                   }

                          The declaration


                   int month_days[2][13] = {
                            {0,31,28,31,30,31,30,31,31,30,31,30,31},
                            {0,31,29,31,30,31,30,31,31,30,31,30,31}};
   91   92   93   94   95   96   97   98   99   100   101