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.