Page 99 - Programming Microcontrollers in C
P. 99

84     Chapter 2  Advanced C Topics

                          3. Write a function that will calculate the vector product of two 1 by
                            4 matrices. The vector product of two arrays is the sum of the
                            products of the corresponding elements of the two arrays.

                          4. Write a function that will calculate the matrix product of two 4 by
                            4 matrices. The matrix product is a matrix whose i,j element is
                            the vector product of the ith row of the first matrix and the jth
                            column of the second matrix. As such, the product of two 4 by 4
                            matrices is a 4 by 4 matrix.


                              C has pointers to functions. A common use for pointers for func­
                          tions is in creating vector tables for microcontrollers. Most
                          microcontroller applications involve the use of interrupts. When an
                          interrupt occurs, the machine status is saved, and program control is
                          transferred to an interrupt service routine. At the close of the inter­
                          rupt service routine, the machine status is returned to the earlier
                          condition, and control is returned to the interrupted program. An
                          address table in memory called the vector table contains the addresses
                          of each interrupt service routine. It is the programmer’s responsibil­
                          ity to fill the vector table with the proper addresses for the various
                          interrupt service routines. Pointers to functions allow the program­
                          mer access to the addresses of the interrupt service routines. We will
                          see several different methods to create the vector tables in the chap­
                          ters on the individual microcontrollers that follow.
                              A pointer to a function is identified by
                   int (*function_ptr)();

                              The above declaration says that function_ptr is a pointer to a
                          function that returns a type int. The arguments are not declared here.
                          The parentheses surrounding *function_ptr are required. If they
                          were not included, the declaration would declare function_ptr as
                          a function that returns a pointer to the type int. If function_ptr
                          is a valid pointer to a function, the function can be accessed by

                   (*function_ptr)(args);
                              The above declaration form can be used for arrays as well as
                          functions. The declaration
                   char (*array_ptr)[];
   94   95   96   97   98   99   100   101   102   103   104