【问题标题】:I'm having trouble with arrays of function pointers in C我在使用 C 中的函数指针数组时遇到问题
【发布时间】:2016-03-19 16:07:09
【问题描述】:

我正在尝试编写一个愚蠢的小程序,但我碰壁了。有问题的代码在这里:

double(*operf[NOPERS])() = {addf,subf,mulf,divf}

我也是这样做的

double(*operf[NOPERS])(double,double) = {addf,subf,mulf,divf}

当我在 main 中使用 printf("%f\n", (*operf[0])(2,3)); 运行程序时,我得到了预期的结果 (5),但是当我从另一个地方调用它时,我得到了 gobbletygook。我知道这在 C 中是可能的,但我不知道我做错了什么。我认真查看了所有其他答案,他们似乎正在做我正在做的事情。

编辑: 这是有问题的代码。不想把你们都淹没在代码中,所以我有点反其道而行,哈哈。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// #include <math.h>

#define SE(X,Y) (strcmp(X,Y)==0)

#define STACKSIZE 1024
double snums[STACKSIZE];
int snums_ctr=0;
int sopers[STACKSIZE];
int sopers_ctr=0;

#define Push(STACK, DATA) STACK[STACK##_ctr++]=DATA
#define Peek(STACK) STACK[STACK##_ctr-1]
#define Pop(STACK) STACK[--STACK##_ctr]

#define ABF(NYM,DEF) double NYM (a,b){ return DEF ; }
ABF(addf,a+b);
ABF(subf,a-b);
ABF(mulf,a*b);
ABF(divf,a/b);
//ABF(pwrf,pow(a,b));

int indexOf(char**ss,char*s) {
    int i=0;
    while(*ss){
        if(SE(*ss,s)) {
            return i;
        }
        i++;ss=&ss[1];
    }
    return -1;
}

#define NOPERS 5

int operp[NOPERS] = {1,1,2,2,3};
int operprec[NOPERS] = {0,0,0,0,1};
char* opers[NOPERS+1] = {"+","-","*","/","**"};
char* cs(char* s) {
    int n=strlen(s);
    char*r=malloc(n+1);
    memcpy(r,s,n+1);
    return r;
}

char* gs(int n) {
    char c = getchar();
    char*r;
    if(c=='\n'){
        c=0;
        r=malloc(n+1);
    }
    else{
        r=gs(n+1);
    }
    r[n]=c;
    return r;
}
typedef double(*oper_f)();

void rpn(oper_f* operf) {
    printf("Entering RPN mode...\n");
    while(1) {
        char* raw = gs(0);
        int idx = indexOf(opers, raw);
        if(idx != -1) {
            double b = Pop(snums);
            double a = Pop(snums);
            double c = (*operf[idx])(b,a);

            printf("%f %s %f = %f %f\n", a,raw,b,(float)(double)c, addf(2,2));
            Push(snums, c);
        }
        else {
            Push(snums, atof(raw));
        }
        free(raw);
    }
}


int main() {
// operf[4] = &pwrf;
    oper_f operf[NOPERS] = {&addf,&subf,&mulf,&divf,NULL};
    printf("%f\n", (*operf[0])(2,3));
    printf("MODE? ");
    char* mode = gs(0);
    if(SE(mode,"rpn")||1) {
        rpn(operf);
    }
    free(mode);
}

【问题讨论】:

  • 没有minimal reproducible example 很难提供任何帮助。
  • 您能否发布代码,或者至少解释一下您在哪里声明指针数组以及它是如何传递给调用它的函数的?没有最少的上下文就很难提供帮助。
  • 编译时有警告吗?
  • 没有,很遗憾。
  • @Mr.Branch 我刚刚添加了所有相关代码。

标签: c function function-pointers


【解决方案1】:

好的,我想通了。这是一个非常愚蠢的错误。我输入了:

#define ABF(NYM,DEF) double NYM (a,b){ return DEF ; }

我应该在什么时候输入

#define ABF(NYM,DEF) double NYM (double a,double b){ return DEF ; }

按照建议添加std=99 有助于我诊断此问题。我想我需要睡觉了。

【讨论】:

  • 很高兴知道您能够修复它!
【解决方案2】:

我通常这样做的方式是:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

typedef float (*math_op)(float, float);

float addf(float f1, float f2) { return (f1 + f2);}
float multf(float f1, float f2) { return (f1 * f2);}
float divf(float f1, float f2) { return (f2 != 0 ? f1/f2 : NAN);}
float subf(float f1, float f2) { return (f1 - f2);}

int main(void)
{
  math_op opsf[] = {addf, multf, divf, subf};

  printf("sum of 1.0 and 3.5 is %f\n", opsf[0](1.0, 3.5));

  return 0;
}

上面的代码在 Windows 7 上使用 gcc-4.8.3 使用命令行gcc -std=c99 -pedantic -Wall temp.c -o temp 编译干净。并给出以下输出:

Q:\>gcc -std=c99 -pedantic -Wall temp.c -o temp
Q:\>temp.exe
sum of 1.0 and 3.5 is 4.500000
Q:\>

如果您仍然遇到问题,请提供您正在使用的系统和编译器,以及您的代码副本。

【讨论】:

    【解决方案3】:

    函数数组示例:

    int f() {
      return 1;
    }
    
    int g() {
      return 2;
    }
    
    typedef int (*PFUNC)();
    
    int main() {
      PFUNC pf[2] = {&f, &g};
      pf[0](); // return 1
    }
    

    【讨论】:

    • 我刚试过。由于某种原因,它根本不起作用。如果有帮助,当我尝试在 main 之外初始化数组时,我会收到“在加载时不可计算”错误,但是当我在 main 中初始化时,这不会影响它。
    • “没有用”是什么意思?它没有编译吗?你的系统和编译器是什么?
    • Linux Mint 17.3,gcc,标准的东西。在示例运行中,我得到的输出是 2.000000 + 3.000000 = 20655140.000000
    猜你喜欢
    • 2021-11-29
    • 1970-01-01
    • 2015-01-07
    • 1970-01-01
    • 1970-01-01
    • 2021-12-05
    • 1970-01-01
    • 2016-02-06
    • 1970-01-01
    相关资源
    最近更新 更多