繁体   English   中英

我在C中使用函数指针数组遇到麻烦

[英]I'm having trouble with arrays of function pointers in C

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

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);
}

好吧,我知道了。 这是一个非常愚蠢的错误。 我输入:

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

我应该什么时候打字

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

按照建议添加std=99可以帮助我诊断此问题。 我想我需要睡觉。

函数数组示例:

int f() {
  return 1;
}

int g() {
  return 2;
}

typedef int (*PFUNC)();

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

我通常这样做的方式是:

#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:\>

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM