views:

199

answers:

4

I have this code:

#include <stdio.h>

int getAns(void);
int num;

int main() 
{
    int (*current_ans)(void);
    current_ans = &getAns;
    // HERE
    printf("%d", current_ans()); 

}

int getAns()
{
    return num + 3;
}

However, is it possible to have something in the // HERE spot that allows the next line to be printf("%d", current_ans); which accesses getAns() in a roundabout way?

A: 

What about "#define current_ans myRoundaboutFnName()"

sjBeta
+4  A: 

I suspect you can not , because the () is necessary to tell the compiler it is an function call. However, if you really want to do it,you can do :

#define current_ans current_ans()
pierr
+5  A: 

Though I agree with pierr's answer, the

#define current_ans current_ans()

will make the code very much unreadable

Alphaneo
+1. And actually putting that macro will make the `main` code garbage.
Johannes Schaub - litb
A: 

No because current_ans and current_ans() are different things. Without the parenthesis, both current_ans and getAns are a function pointers, while with parenthesis, they are function calls. If you think of the parenthesis as sort of dereferencing the pointer by executing the code, essentially what you are asking is to treat the pointer and the contents of the pointer as one in the same.

ctuffli