Thread: Muting from functions to pointers to functions

  1. #1
    Registered User
    Join Date
    May 2015
    Posts
    90

    Muting from functions to pointers to functions

    Hey, well the question is basically what the title states, If I have one function and I want to call a subfunction to do something, how do I mute from having a function call and then one or the other function call inside it and using a pointer to a function?
    That was barely understandable so I'll make myself clearer:
    Suppose I have this:
    Code:
    status_t merge_files(FILE *ft, FILE *fi, FILE *fc, size_t i)
    {
    
        /* fe = file error */
        FILE *fe = NULL;
        
        if((fe = fopen(FILE_ERROR, "wb")) == NULL) {
            return ERROR_OPEN_FILE;
        }
        if(i == 1) {
            merge_inv_wer(fi, fc, ft, fe);
        } else if(i == 2) {
            merge_inv_nwer(fi, fc, ft, fe);
        } else {
            merge_inv_rwer(fi, fc, ft, fe);
        }
        
        if(fclose(fe) == EOF) {
            return ERROR_WRITE_FILE;
        }
        return OK;
    }
    A function that merges two files and put it in a new file
    Where:

    • fi -> first file
    • fc -> second file
    • ft -> new file (temporal)
    • fe -> possible error file

    I want to do this exact same thing, but instead of having to check from the inside of the function, I'd like to check from the outside and call the function like:
    Code:
    merge_files(fi, fc, ft, fe, merge_inv_wer);
    or
    Code:
    merge_files(fi, fc, ft, fe, merge_inv_nwer);
    if neccessary.

    Now from what I understand, to do this I must use pointers to functions. I'm not really sure how to even start to mute my functions and make pointers out of them?
    I know merge_file would look something like:
    Code:
    status_t merge_file(FILE *fi, FILE *fc, FILE *ft, FILE *fe, (*pf)(FILE *fi, FILE *fc, FILE *ft, FILE *fe))
    W/ maybe different params cause that's troubling.
    Now how am I supposed to "define" my functions as pointers to functions and how do I mute my function to make use of it.

    I hope some of it at least made sense.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    in most cases I saw usage of function pointers - typedef is used to simplify the syntax

    Code:
    typedef status_t (*merge_inv_t)(FILE *ft, FILE *fi, FILE *fc,FILE *fe);
    
    status_t merge_files(FILE *ft, FILE *fi, FILE *fc, merge_inv_t f)
    {
    ...
    if(f)
       return (*f)(ft,fi,fc,fe);
    }
    You have nothing to do to "make pointers out of function" it is all done automatically with the code you have posted

    Code:
    merge_files(fi, fc, ft, fe, merge_inv_nwer);
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    May 2015
    Posts
    90
    Quote Originally Posted by vart View Post
    in most cases I saw usage of function pointers - typedef is used to simplify the syntax

    Code:
    typedef status_t (*merge_inv_t)(FILE *ft, FILE *fi, FILE *fc,FILE *fe);
    
    status_t merge_files(FILE *ft, FILE *fi, FILE *fc, merge_inv_t f)
    {
    ...
    if(f)
       return (*f)(ft,fi,fc,fe);
    }
    You have nothing to do to "make pointers out of function" it is all done automatically with the code you have posted

    Code:
    merge_files(fi, fc, ft, fe, merge_inv_nwer);
    Uhm, well I gave it a try but the code didn't do anything, no errors though.
    I typdefed:
    Code:
    typedef status_t (*merge_inv_t)(FILE *ft, FILE *fi, FILE *fc,FILE *fe);
    And then changed the code so it looks like this now:
    Code:
    status_t merge_files(FILE *ft, FILE *fi, FILE *fc, merge_inv_t pf){
        /* fe = file error */
        FILE *fe = NULL;
        
        if((fe = fopen(FILE_ERROR, "wb")) == NULL) {
            return ERROR_OPEN_FILE;
        }
        (*pf)(ft, fi, fc, fe);
        
        if(fclose(fe) == EOF) {
            return ERROR_WRITE_FILE;
        }
        return OK;
    }
    And passed the arg when calling the function:
    Code:
    ...
    if((st = merge_files(ft, fi, fc, merge_inv_nwer)) != OK) {
        return EXIT_FAILURE;
    }

    I posted right away since maybe there is something definetely wrong that I'm not seeing at all.. If there isn't I'll try to debug it and see what the heck the prog is doing, but right now is there something wrong?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Functions and pointers
    By DOMINIC in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2005, 03:05 PM
  2. Functions and pointers.
    By Hulag in forum C++ Programming
    Replies: 10
    Last Post: 03-28-2005, 08:46 PM
  3. Pointers to functions
    By Thantos in forum C Programming
    Replies: 2
    Last Post: 05-03-2004, 03:36 PM
  4. Replies: 1
    Last Post: 01-20-2002, 11:50 AM
  5. Pointers to functions
    By Leeman_s in forum C++ Programming
    Replies: 1
    Last Post: 12-24-2001, 09:27 PM