| 
 | 
 
10鱼币 
 
按函数指针的课后习题改的,编译能过,运行时会跳出问题,非常疑惑 
直接fp=add等是可以运行的 
#include<stdio.h> 
float add(float, float); 
float sub(float, float); 
float mul(float, float); 
float chu(float, float); 
float (*sellect(char op))(float, float); 
float calc(float*fp(float,float),float ,float); 
 
float add(float num1, float num2) 
{ 
        return num1 + num2; 
} 
 
float sub(float num1, float num2) 
{ 
        return num1 - num2; 
} 
 
float mul(float num1, float num2) 
{ 
        return num1 * num2; 
} 
 
float chu(float num1, float num2) 
{ 
        return num1 / num2; 
} 
float (*sellect(char op))(float, float) 
{ 
        switch ('op') 
        { 
        case'+':return add; 
        case'-':return sub; 
        case'*':return mul; 
        case'/':return chu; 
        default:  
                return 0; 
        } 
} 
 
float calc(float(* fp)(float, float), float num1, float num2) 
{ 
//        return (*fp)(num1, num2); 
} 
 
int main(void) 
{ 
        float num1, num2; 
        char op; 
        float(*fp)(float,float); 
        printf("请输入+-*/的算法式例如(3+5)\n"); 
        scanf("%f%c%f",&num1,&op,&num2); 
        fp = sellect(op); 
 
        printf("%f%c%f=%f", num1, op, num2,(*fp)(num1,num2));//在本行跳出0x00000000 处(位于 Project1.exe 中)引发的异常: 0xC0000005: 执行位置 0x00000000 时发生访问冲突 
        return 0; 
} 
 本帖最后由 bin554385863 于 2019-12-5 13:00 编辑 
指出你不写注释就口嗨? 
厉害啊,我竟无言以对!!!NX
 - #include <stdio.h>
 
 - float add(float a, float b)//计算加法
 
 - {
 
 -     return a + b;
 
 - }
 
 - float sub(float a, float b)//计算减法
 
 - {
 
 -     return a - b;
 
 - }
 
 - float mul(float a, float b)//计算乘法
 
 - {
 
 -     return a * b;
 
 - }
 
 - float div(float a, float b)//计算除法
 
 - {
 
 -     return a / b;
 
 - }
 
 - float (*sellect(char op))(float, float)//返回指定类型的函数指针
 
 - {
 
 -     float (*funcptr)(float, float);
 
 -     switch (op)
 
 -     {
 
 -     case '+':
 
 -         funcptr = add;
 
 -         break;
 
 -     case '-':
 
 -         funcptr = sub;
 
 -         break;
 
 -     case '*':
 
 -         funcptr = mul;
 
 -         break;
 
 -     case '/':
 
 -         funcptr = div;
 
 -         break;
 
 -     }
 
 -     return funcptr;
 
 - }
 
 - float calc(float a, char op, float b, float (*funcptr)(float, float))//计算函数
 
 - {
 
 -     funcptr = sellect(op);
 
 -     return funcptr(a, b);
 
 - }
 
 - int main(int argc, char const *argv[])
 
 - {
 
 -     float a = 5, b = 7;
 
 -     char s = '+';
 
 -     float (*funcptr)(float, float);
 
 -     calc(a, s, b, funcptr);
 
 -     printf("%.2f %c %.2f = %.2f\n", a, s, b, calc(a, s, b, funcptr));
 
 -     s = '-';
 
 -     calc(a, s, b, funcptr);
 
 -     printf("%.2f %c %.2f = %.2f\n", a, s, b, calc(a, s, b, funcptr));
 
 -     s = '*';
 
 -     calc(a, s, b, funcptr);
 
 -     printf("%.2f %c %.2f = %.2f\n", a, s, b, calc(a, s, b, funcptr));
 
 -     s = '/';
 
 -     calc(a, s, b, funcptr);
 
 -     printf("%.2f %c %.2f = %.2f\n", a, s, b, calc(a, s, b, funcptr));
 
 -     return 0;
 
 - }
 
 
  复制代码
================================================
 Microsoft Windows [版本 10.0.18363.476] 
(c) 2019 Microsoft Corporation。保留所有权利。 
 
E:\Users\admin\Documents\VScode\Code>c:\Users\admin\.vscode\extensions\ms-vscode.cpptools-0.26.2\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-3xhsedah.zwn --stdout=Microsoft-MIEngine-Out-4exwmvoh.unt --stderr=Microsoft-MIEngine-Error-5cj4vc0c.jga --pid=Microsoft-MIEngine-Pid-ckhutod3.xho --dbgExe=D:\MinGW\bin\gdb.exe --interpreter=mi 
5.00 + 7.00 = 12.00 
5.00 - 7.00 = -2.00 
5.00 * 7.00 = 35.00 
5.00 / 7.00 = 0.71 
 
E:\Users\admin\Documents\VScode\Code> 
 
 
 |   
 
 
最佳答案
查看完整内容 
指出你不写注释就口嗨?
厉害啊,我竟无言以对!!!NX
================================================
Microsoft Windows [版本 10.0.18363.476]
(c) 2019 Microsoft Corporation。保留所有权利。
E:%users\admin\Documents\VScode\Code>c:%users\admin\.vscode\extensions\ms-vscode.cpptools-0.26.2\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-3xhsedah.zwn --stdout=Microsoft-MI ... 
 
 
 
 
 
 
 |