PROGRAMMING:Pointer code analysis of pointing function
```
Code analysis pointer to function code analysis
Analyze the following code to understand function pointer variables.
#include
int add(int a,int b){return a+b;}
int sub(int a,int b){return a-b;}
int mul(int a,int b){return a*b;}
int div(int a,int b){
if(b==0){
printf("E rror:Divide by zero.");
exit(0);
}
return a/b;
}
int error(int a,int b){
printf("E rror:Expression undefined! ");
exit(0);
}
int main(){
int x,y,z;
char op='#';
int(*fun)(int,int);
scanf("%d%c%d",&x,&op,&y);
switch(op){
case '+': fun=add; break;
case '-': fun=sub; break;
case '*': fun=mul; break;
case '/': fun=div; break;
default: fun=error;
}
z=(*fun)(x,y);
printf("Result=%d\n",z);
return 0;
}
Execute program input:
1+2
Output:
Result=3
Execute the input again:
9-8
Output:
Result=1
Execute the input again:
9/0
Output:
E rror:Divide by zero.
Execute the input again:
1H2
Output:
E rror:Expression undefined!
```
###Input example:
```in
1+2
```
###Output example:
```out
Result=3
```
answer:If there is no answer, please comment
Code analysis pointer to function code analysis
Analyze the following code to understand function pointer variables.
#include
int add(int a,int b){return a+b;}
int sub(int a,int b){return a-b;}
int mul(int a,int b){return a*b;}
int div(int a,int b){
if(b==0){
printf("E rror:Divide by zero.");
exit(0);
}
return a/b;
}
int error(int a,int b){
printf("E rror:Expression undefined! ");
exit(0);
}
int main(){
int x,y,z;
char op='#';
int(*fun)(int,int);
scanf("%d%c%d",&x,&op,&y);
switch(op){
case '+': fun=add; break;
case '-': fun=sub; break;
case '*': fun=mul; break;
case '/': fun=div; break;
default: fun=error;
}
z=(*fun)(x,y);
printf("Result=%d\n",z);
return 0;
}
Execute program input:
1+2
Output:
Result=3
Execute the input again:
9-8
Output:
Result=1
Execute the input again:
9/0
Output:
E rror:Divide by zero.
Execute the input again:
1H2
Output:
E rror:Expression undefined!
```
###Input example:
```in
1+2
```
###Output example:
```out
Result=3
```
answer:If there is no answer, please comment