简体   繁体   中英

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

I'm trying to write a silly little program, and I've run into a wall. The code in question is here:

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

Which I've also done as

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

When I run the program in main, using printf("%f\\n", (*operf[0])(2,3)); , I get the expected result (5), but when I call it from another place, I get gobbletygook. I know that this is possible in C and I don't know what I'm doing wrong. I've seriously looked at all of the other answers, and they seem to be doing exactly what I'm doing.

EDIT: Here's the code in question. Didn't want to drown you all in code so I sort of went the opposite way haha.

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

Okay, I figured it out. It was a really silly error. I typed:

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

When I should have typed

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

Adding std=99 as suggested helped me diagnose this problem. I think I need sleep.

Example for array of funcs:

int f() {
  return 1;
}

int g() {
  return 2;
}

typedef int (*PFUNC)();

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

The way that I normally do this is:

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

The above code compiles cleanly on Windows 7 using gcc-4.8.3 using the command line gcc -std=c99 -pedantic -Wall temp.c -o temp . And give the following output:

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

If you are still having problems, please provide the system and compiler you are using, as well as a copy of your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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