|
|||||||||||
Technical Support On-Line Manuals Cx51 User's Guide Cx51 Introduction Compiling Programs Language Extensions Preprocessor Advanced Programming 8051 Derivatives Error Messages Library Reference Appendix A. ANSI C Implementation Implementation-defined Behavior Compiler-Related Library-Related B. Version Differences Version 6.0 Differences Version 5.0 Differences Version 4.0 Differences Version 3.4 Differences Version 3.2 Differences Version 3.0 Differences Version 2.0 Differences C. Writing Optimum Code Memory Model Variable Location Variable Size Unsigned Types Local Variables Other Sources D. Compiler Limits E. Byte Ordering F. Hints, Tips, and Techniques Recursive Code References Problems Using printf Uncalled Functions Using Monitor-51 Trouble with bdata Function Pointers |
Function PointersFunction pointers are one of the most difficult aspects of C to understand and to properly utilize. Most problems involving function pointers are caused by improper declaration of the function pointer, improper assignment, and improper dereferencing. The following brief example demonstrates how to declare a function pointer (f), how to assign function addresses to it, and how to call the functions through the pointer. The printf routine is used for example purposes when running the µVision Debugger to simulate program execution. #pragma code symbols debug oe #include <reg51.h> /* special function register declarations */ #include <stdio.h> /* prototype declarations for I/O functions */ void func1(int d) { /* function #1 */ printf("In FUNC1(%d)\n", d); } void func2(int i) { /* function #2 */ printf("In FUNC2(%d)\n", i); } void main(void) { void (*f)(int i); /* Declaration of a function pointer */ /* that takes one integer arguments */ /* and returns nothing */ SCON = 0x50; /* SCON: mode 1, 8-bit UART, enable rcvr */ TMOD |= 0x20; /* TMOD: timer 1, mode 2, 8-bit reload */ TH1 = 0xf3; /* TH1: reload value for 2400 baud */ TR1 = 1; /* TR1: timer 1 run */ TI = 1; /* TI: set TI to send first char of UART */ while( 1 ) { f = (void *)func1; /* f points to function #1 */ f(1); f = (void *)func2; /* f points to function #2 */ f(2); } } Note
| ||||||||||
|
Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.