Search code examples
cfunction-pointers

Correct syntax use function pointers in C


I have read about syntax of function pointers and I can't make to work in my code.

This video explains that the syntax is:

type func0(type0 arg0, ..., typen argn){
    // do something
    return a_thing;
}

Then for passing function pointers in another function is:

type func1((*func0)(type0 arg0, ..., typen argn), another args){  
    //do something
    func0(arg0, ..., argn);    
    return a_thing;
}

But then, I read the first answer about How do function pointers in C work?, and the syntax is:

type func1((*func0)(type0 arg0, ..., typen argn), another args) {    
    //do something
    (*func0)(arg0, ..., argn);
    return a_thing;
}

which makes sense because you are dereferencing the pointer, which you are passing. Lastly, the first answer in C function pointers invocation syntax explain that the syntax can be:

&func
func
*func
**func

and I don't understand which info is correct. I have the code:

double *func1((*func0)(double), double *var) {
    double *temp = malloc(some_size);
    for(int i = 0; i < some_cycles; ++i) {
        temp[i] = (*func0)(var[i]);
    }
    return temp;
}

And it throws the error a value of type "double *" cannot be assigned to an entity of type "double"

EDIT:

For anyone wanted to see the real code, was that:

tensor_t *applyBinOpTensor(double *(operation)(double, double), tensor_t *tensor, double operand){

    size_t total_size = indiceProduct(*tensor->shape);
    double *values = malloc(total_size * sizeof(*values));

    for(size_t i = 0; i < total_size; ++i){
        values[i] = (*operation)(tensor->values[i], operand);
    }
    return initializeTensor(values, tensor->shape);
}

As you can see, all the problem was that I typed double *(operation)(double, double) instead of double (*operation)(double, double).

Thanks to all for your answers. I learned a lot, and sorry for my misspeling.


Solution

  • To pass a function pointer to a function is relatively easy.

    Given some function pointer:

    int (*fptr) (int, double);
    

    Just squeeze the same syntax into the parameter list:

    void some_func (int some_param, int (*fptr)(int, double), int some_other_param);
    

    It turns more confusing when you try to return a function pointer. Lets take the above function and move the function pointer from the parameter list to the return type:

    int (*some_func(int some_param, int some_other_param)) (int, double);
    

    Yep, unreadable gibberish...


    Now what can we actually learn from this?

    • Whoever invented the function pointer syntax in C were confused.
    • People who write code like the above are confused.
    • People who try to learn function pointer syntax will get confused.
    • If you have actually learnt the syntax, you have spent way too much time on nonsense, that could have been spent on better things.

    Always use typedef. Period.

    typedef int func_t (int, double);
    
    void some_func (int some_param, func_t* fptr, int some_other_param);
    
    func_t* some_func (int some_param, int some_other_param);