Thread: function pointer to macro, possible?

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    2

    function pointer to macro, possible?

    Hi all,

    Is it possible to use a function pointer "pointing" to a macro which expands to the actual function?

    Look at the following dummy code:
    Until now I just called the functions Abstr_0_SendData() and Abstr_1_SendData() directly, now I rewrote the code to call them via a function pointer configured in the myconfig var, is this somehow possible? It gives me some errors, see below.

    Code:
    #include <stdio.h>
    
    #define Abstr_0_SendData(data) SendData(0, data)
    #define Abstr_1_SendData(data) SendData(1, data)
    
    typedef void (*fptr)(int data);
    //typedef void (*fptr)(int type, int data);   // 2nd fptr try
    
    typedef struct
    {
        int type;
        fptr my_fkt;
    } config;
    
    void SendData(int type, int data)
    {
           printf("SendData: %d, %d\n", type, data); 
    }
    
    int main ()
    {
        int i;
    
        config myconfig[2] = 
            { { 10, &Abstr_0_SendData }   
            , { 20, &Abstr_1_SendData }        
            };
        
        for (i = 0; i < 2; i++) {
            myconfig[i].my_fkt(99);
            // int type = myconfig[i].type;  
            // myconfig[i].my_fkt(type, 99);   // 2nd fptr try
        }
    
        return 0;
    }
    which results in:
    main.c: In function `main':
    main.c:25: error: `Abstr_0_SendData' undeclared (first use in this function)
    main.c:25: error: (Each undeclared identifier is reported only once
    main.c:25: error: for each function it appears in.)
    main.c:26: error: `Abstr_1_SendData' undeclared (first use in this function)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by edwood
    Is it possible to use a function pointer "pointing" to a macro which expands to the actual function?
    No, since a function-style macro is not a function.

    Quote Originally Posted by edwood
    Until now I just called the functions Abstr_0_SendData() and Abstr_1_SendData() directly, now I rewrote the code to call them via a function pointer configured in the myconfig var, is this somehow possible?
    Why not write functions that call SendData?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    2
    Quote Originally Posted by laserlight View Post
    Why not write functions that call SendData?
    the problem is that these macros are automatically generated from a code generator and once these Abstr_x_SendData() functions are generated as Macros (like above) and once they are generated as normal functions with additional code before calling SendData(), this depends on the configuration of the code generator which changes for every project, furthermore these macros should abstract SendData(), so I usually don't know all of the used function parameters.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    A #defined macro is inserted inline in the source by the preprocessor. At runtime there is nothing left of the macro, and it's name. It's therefore also not possible to reference it with a pointer, since it doesn't exist.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You're out of luck, it can't be done.
    So your only option is to change your mind and do what Laserlight suggests, or just forget the idea.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Write two functions that call the MACROs.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Or, write a macro that expands to a function definition. It's not that hard, given that macros are just text substitution at preprocessor time. Then your "automated" generation actually makes uniquely-named functions, as many as required, instead of expanding to a function call of something else, and you can take the pointers to them quite easily.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Subtracting a macro from a pointer
    By limp in forum C Programming
    Replies: 2
    Last Post: 05-24-2011, 06:38 AM
  2. macro function
    By freeindy in forum C Programming
    Replies: 6
    Last Post: 11-08-2007, 05:04 AM
  3. Macro has same name as Function?
    By coolclu3 in forum C Programming
    Replies: 10
    Last Post: 09-28-2007, 04:55 AM
  4. macro function
    By bradleyd in forum C Programming
    Replies: 8
    Last Post: 05-21-2007, 05:18 PM
  5. macro or function
    By studentc in forum C Programming
    Replies: 5
    Last Post: 06-02-2004, 01:40 PM