Zhou Ligong teaches you to learn C language programming and programming: this is best to write a function pointer array.

Chapter two is about programming technology. In this section, we will explore the concept of function pointer arrays, which is a crucial aspect of advanced programming. For instance, imagine you're building a pocket calculator that reads two numbers, op1 and op2, along with an operator, oper. Traditionally, one might use a switch statement to check the operator and decide which function to call: ```c++ switch(oper) { case '+': result = op1 + op2; break; case '-': result = op1 - op2; break; //... and so on for many more cases } ``` Clearly, for a calculator with hundreds of operators, this approach becomes cumbersome and inefficient. Instead, you could utilize a transfer table, which is essentially an array of function pointers. Each element in this array would hold the address of a specific function. Let’s define a pointer type PF for a function that returns a double and takes two double parameters: ```c++ typedef double (*PF)(double, double); ``` Now, suppose you need to declare an array called `oper_func` with four elements to store the addresses of four functions. Here's how you could define such an array: ```c++ PF oper_func[4] = {ADD, SUB, MUL, DIV}; ``` In this example, `PF` defines the type of the array elements as pointers to functions, while `oper_func` is an array of these function pointers. Even though the syntax looks slightly different, the functionality remains identical. Assigning values to elements in a function pointer array works just like assigning values to elements in a regular array. For example: ```c++ oper_func[0] = ADD; ``` This is similar to assigning any other array element. Function pointer arrays can also be initialized during their declaration: ```c++ PF oper_func[] = {ADD, SUB, MUL, DIV}; ``` When creating a transfer table, it’s important to ensure that the prototypes of the functions being pointed to are declared before the array itself. Here's an example: ```c++ double ADD(double a, double b) { return a + b; } double SUB(double a, double b) { return a - b; } double MUL(double a, double b) { return a * b; } double DIV(double a, double b) { return a / b; } PF oper_func[] = {ADD, SUB, MUL, DIV}; ``` In this case, the initial values in the array automatically determine the size of the array. So, there’s no need for a separate initialization step. It’s critical that the order of the function names in the initialization list matches the integer codes used by the program to represent each operator. For instance, ADD, SUB, MUL, and DIV might correspond to 0, 1, 2, and 3, respectively. The function call would look like this: ```c++ result = oper_func[oper](op1, op2); ``` Here, the `oper` variable selects the appropriate function pointer from the array, and the function call operator executes the selected function. Alternatively, you can omit the size of the array and let the compiler determine it based on the initialization list: ```c++ PF oper_func[] = {ADD, SUB, MUL, DIV}; ``` The number of initial values in the braces will define the size of the array. This method applies equally well to initializing other types of arrays. For further reading, if you text the keyword **[programming]** in the background of our public account, you can access two books: *"Programming for AMetal Framework and Interface (Top)"* and *"Programming and Data Structures"*. Additionally, the book *"Programming for AMetal Frameworks and Interfaces (I)"* is currently in the printing phase and will be announced upon release. The Taobao link for purchasing *"Programming and Data Structures"* is as follows, and can be opened directly in your browser: **[https://s.click.taobao.com/d3WStbw](https://s.click.taobao.com/d3WStbw)** --- **Extended Reading:** - Zhou Ligong: A Callback Function That Must Be Mastered in Software Module Layered Design - Zhou Ligong: Dynamically Distributed Memory - free() Function and realloc() Function - Zhou Ligong: Dynamically Distributed Memory - malloc() Function and calloc() Function - Zhou Ligong: Constructing a Double Pointer with an Array - Zhou Ligong: Do You Know That Arrays Can Also Save Pointers? - Zhou Ligong: The True Value of Characters Lies in the Formation of Character Sequences - String Functions - Zhou Ligong: The Real Value of Characters Lies in the Formation of Character Sequences - Input and Output of Strings - Zhou Ligong: The True Value of Characters Lies in the Formation of Character Sequences - Reference to a String - Zhou Ligong: Characters Can Be Calculated Like Integers - Zhou Ligong: How to Pass a Two-Dimensional Array as a Function Parameter - Zhou Ligong: Generalization of Algorithms, You Should Know - Zhou Ligong: Generic Programming - Implementing Code Reuse - Zhou Ligong: Four Methods and Idioms for Array Access - Zhou Ligong: The Rules of the Array, Do You Know? - Zhou Ligong: C Language Array and Pointer Detailed - Zhou Ligong: Implementing and Using an Easy-to-Use Abstract Interface - Zhou Ligong: Five Things to Pay Attention to When Designing a Good Program Interface - Zhou Ligong: New Thinking and New Methods of Programming - Zhou Ligong: Splitting Conditional Statements Makes Code Easier to Understand - Comprehensive Expressions and Conditional Expressions - Zhou Ligong: Splitting Conditional Statements Makes Code Easier to Understand - Logical Expressions --- **Introduction to the Public Account:** The Zhiyuan Electronics official WeChat public account is a research and development testing sharing platform for 500 engineers. It provides leading product technology and solutions in the electronics industry. ![Image](http://i.bosscdn.com/blog/14/09/11/5034-8.gif)

Forging Parts

Forging is a processing method that uses a forging machine to apply pressure to a metal blank to plastically deform it to obtain a forging with certain mechanical properties, certain shape and size. One of the two major components of forging (forging and stamping)

Forging Parts,Steel Structure Part,Die Hot Forged Part,Cnc Machined Part

Tianhui Machine Co.,Ltd , https://www.thcastings.com