Thread: tricky line of code

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    269

    tricky line of code

    Not sure what this is, but maybe it's a macro??

    I'm new to C and am working on a project written entirely in C and fortran. In this project, I repeatedly see calls to fork_thread(....). I tried to find out where this fork_thread is defined, but all I could find was this in the header file... the .c file has more normal code

    Code:
    typedef int (*void_arg_func) ARGS((void *));
    
    #define fork_thread(funct, args) ((libgen_init()->fork_func) ? libgen_init()->fork_func(func, arg) : NULL)
    Here is how the function gets called in the other files:
    Code:
    fork_thread((void_arg_func) calc_center_thread, NULL);
    calc_center_thread is a function pointer.


    Basically, my question is, what is that called that's declaring fork_thread? Is that a macro? I've never seen anything like that before.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    269
    Also, just to be complete, here is the code from the .c file

    Code:
    extern thr_thread_t 
    fork_thread(func, arg, node)
    void_arg_func func;
    void *arg;
    int node;
    {
        if (gen_thr_info.fork_func) {
    	return gen_thr_info.fork_func(func, arg, node);
        } else {
    	return NULL;
        }
    }
    gen_thr_info is just a struct.. but it's calling a function on a struct (fork_func)?

    I'm very confused......

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    269
    Did I give enough information? Perhaps I need to give more?

  4. #4
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    #define fork_thread(funct, args) ((libgen_init()->fork_func) ? libgen_init()->fork_func(func, arg) : NULL)
    Yes, it's a macro.
    "libgen_init()" most likely returns a pointer to struct. And that struct contains a function pointer, not a member function. The syntax of function pointers lets you call a pointer like you would any other function, which leads to some confusion on the users part [ie, "How am I calling a member function in C?"]. But it's perfectly ok.

    So what the macro does is first check if (libgen_init()->fork_func) is 0 or not. It's a function pointer, and it should have been set to 0 in the constructor for the struct; if it's not 0, that means you've set it to point to some function.
    If it is not equal to 0 by virtue of the ternary (a ? b : c) operator, the expression evaluates to (libgen_init()->fork_func(func,arg)), which calls whatever function you defined fork_func to be with appropriate arguments.
    However if it isn't, the expression evaluates to NULL.
    Last edited by bernt; 09-10-2010 at 02:13 PM. Reason: edited for clarity
    Consider this post signed

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    269
    Quote Originally Posted by bernt View Post
    Yes, it's a macro.
    "libgen_init()" most likely returns a pointer to struct. And that struct contains a function pointer, not a member function. The syntax of function pointers lets you call a pointer like you would any other function, which leads to some confusion on the users part [ie, "How am I calling a member function in C?"]. But it's perfectly ok.

    So what the macro does is first check if (libgen_init()->fork_func) is 0 or not. It's a function pointer, and it should have been set to 0 in the constructor for the struct; if it's not 0, that means you've set it to point to some function.
    If it is not equal to 0 by virtue of the ternary (a ? b : c) operator, the expression evaluates to (libgen_init()->fork_func(func,arg)), which calls whatever function you defined fork_func to be with appropriate arguments.
    However if it isn't, the expression evaluates to NULL.
    Excellent. That explains it beautifully. Thank you so much!

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Yes, things beginning with #define are macros.

    > gen_thr_info is just a struct.. but it's calling a function on a struct (fork_func)?
    Look at the structure variable.
    Such things are called function pointers.

    This is pretty heavy stuff for someone new to C, so it's no surprise you're confused at the moment.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Run command line code from within my c code.
    By baikal_m in forum C Programming
    Replies: 6
    Last Post: 01-28-2010, 03:58 PM
  2. Understanding a Line of Quake 2 Source Code
    By bengreenwood in forum C++ Programming
    Replies: 6
    Last Post: 08-04-2009, 03:15 PM
  3. C code line, pointer declaration
    By Dedalus in forum C Programming
    Replies: 2
    Last Post: 06-10-2009, 04:34 AM
  4. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 04:13 PM
  5. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 06:04 AM